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



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

示例下拉列表的项

Value   Text
1       AAA
2       BBBB
3       CCCCC

获取当前选定的文本:

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

显示警报结果

alert(selectedItemText);
// Show "AAABBBBCCCCC"
// But I want "AAAnBBBBnCCCCC" 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>

试试这个: alert("selectedItemValue"(;

试试这个

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

最新更新