Javascript 如何使用Javascript获取所有选定的选项文本表单多选
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14972708/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get all selected option text form multi select Using Javascript
提问by user123
I have problem in get all selected option in multi select
我在多选中获取所有选定选项时遇到问题
<select multiple="" title="" class="" id="fm_delivery_or_collection" name="fm_fields[fm_delivery_or_collection][]">
<option value="90">Delivery or Collection1</option>
<option value="91">Delivery or Collection2</option>
<option value="92">Delivery or Collection3</option>
</select>
Bellow is my code and its return me only first selected option
波纹管是我的代码,它只返回我第一个选择的选项
var select = form.find('select')
for (var i = 0; i < select.length; i++)
{
var s_id = jQuery(select[i]).attr('id');
var str="",i;
var e = document.getElementById(s_id);
var strUser = e.options[e.selectedIndex].text;
var name = jQuery(select[i]).attr('name')
var str1 = jQuery(select[i]).attr('id').replace("fm_"," ")
requestString += "<b>"+str1.replace(/_/g," ")+"</b>" + ':' +strUser+"<br>";
}
So please suggest me how can i get all selected option text and where I make mistake ?
所以请建议我如何获得所有选定的选项文本以及我在哪里出错?
回答by Jai
Your comment please suggest me how can i get all selected option text, So you can try this:
你的评论please suggest me how can i get all selected option text,所以你可以试试这个:
$("#fm_delivery_or_collection option:selected").each(function () {
var $this = $(this);
if ($this.length) {
var selText = $this.text();
console.log(selText);
}
});
回答by peiman F.
do all in one line now
现在在一行中完成所有操作
var text = $('#selector option:selected').toArray().map(item => item.text).join();
it return like this:
它像这样返回:
text1,text2,text3,text4
回答by Talha
// Return an array of the selected opion values
// select is an HTML select element
function GetSelectValues(select) {
var result = [];
var options = select && select.options;
var opt;
for (var i=0, iLen=options.length; i<iLen; i++) {
opt = options[i];
if (opt.selected) {
result.push(opt.value || opt.text);
}
}
return result;
}
pass this function your select box as like..
将此功能传递给您的选择框,就像..
var selectBox = document.getElementsById('fm_delivery_or_collection');
alert(GetSelectValues(selectBox ));
it will return the array of selected values
它将返回选定值的数组
By using Jquery
通过使用 Jquery
$('select#fm_delivery_or_collection').val()
the valfunction called from the selectwill return an array if its a multiple.
如果它是一个倍数,val则从 调用的函数select将返回一个数组。
回答by Gautam3164
Try with this
试试这个
$("#fm_delivery_or_collection option").each(function(){
if($(this).attr('selected') == 'selected')
{
var name = $("#fm_delivery_or_collection").attr('name')
var str1 = $("#fm_delivery_or_collection").attr('id').replace("fm_"," ")
requestString += "<b>"+str1.replace(/_/g," ")+"</b>" + ':' +strUser+"<br>";
}
})
回答by Prabhagaran
$("#id :selected").each(function (i,sel) {
alert($(sel).text());
});
Hope it Helps..
希望能帮助到你..
回答by Muthu
$("#selection option:selected").each(function () {
var $this = $(this);
if ($this.length) {
var selText = $this.text();
console.log(selText);
$('#selected').append(selText+', ');
}
});
回答by user11629449
Simply add this line of code:-
只需添加这行代码:-
var reciever= jQuery('#to_select option:selected').toArray().map(item => item.text).join();
/* alert(reciever); */

