获取下拉列表项的选定文本并按新行拆分,然后使用 jQuery 显示警报结果
示例下拉列表项
Value Text
1 AAA
2 BBBB
3 CCCCC
获取当前选中的文本:
var selectedItemText = $("#dropDownId option:selected").text();
// Get AAABBBBCCCCC
显示警报结果
alert(selectedItemText);
// Show "AAABBBBCCCCC"
// But I want "AAA\nBBBB\nCCCCC" How to split selectedItemText by new line?
抱歉,信息较少,我将文本从“A”更改为“AAA”,将“B”更改为“BBBB”,将“C”更改为“CCCCC”。
最佳答案
试试这个:
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>
关于javascript - 获取下拉列表项的选定文本并按新行拆分,然后使用 jQuery 显示警报结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45913104/