获取下拉列表项的选定文本并按新行拆分,然后使用jQuery显示警报结果

时间:2017-08-28 06:44:03

标签: javascript jquery

获取下拉列表项的选定文本并按新行拆分,然后使用jQuery显示警报结果

示例下拉列表项

f :: forall r. { x :: Int, y :: Int | r } -> { x :: Int, y :: Int | r }
f r = r { x = r.x * 2, y = r.y * 2 }

获取当前选定的文字:

Value   Text
1       AAA
2       BBBB
3       CCCCC

显示提醒结果

var selectedItemText = $("#dropDownId option:selected").text();
// Get AAABBBBCCCCC

我很抱歉找不到更多信息,我将文字从“A”更改为“AAA”,将“B”更改为“BBBB”,将“C”更改为“CCCCC”。

3 个答案:

答案 0 :(得分:2)

试试这个:

alert(selectedItemValue.split('').join('\n'));

更新

$('#dropDownId').change(function(){
var results="";
$('#dropDownId :selected').each(function(index, sel){ 
   results+= $(sel).text()+'\n'; 
});

alert(results);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
<select id="dropDownId" multiple="multiple" name="multiple">
  <option value=""> -- Select -- </option>
  <option value="1">AAA</option>
  <option value="2">BBB</option>
  <option value="3">CCC</option>
 </select>

答案 1 :(得分:0)

试试这个

var str = "";
$("#dropDownId option:selected").each(function () {
        str += $(this).text() + "\n";
      });
});
alert(str);

答案 2 :(得分:0)

试试这个: 警报( “selectedItemValue \ n”);