Html select multiple获取onchange事件的所有值 [英] Html select multiple get all values at onchange event
本文介绍了Html select multiple获取onchange事件的所有值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个选择倍数的表单。我想要在onchange事件中获取所有选定的值,但我不知道这是否可能。我认为this.value只返回所选的最后一个元素。
是否有可能将所有选中的元素作为数组选中?
预先致谢。
< select name =myarray []id =myarrayclass =select2-select reqstyle =宽度:90%; onChange =get_values(this.value)multiple>
{foreach key = key item = value from = $ myarray}
< option value ={$ key}> {$ value}< / option>
{/ foreach}
< / select>
解决方案
function getSelectedOptions(sel){var opts = [],opt; var len = sel.options.length; for(var i = 0; i< len; i ++){opt = sel.options [i];如果(opt.selected){opts.push(opt);警报(opt.value); }} return opts;}
< select name = myarray []id =myarrayclass =select2-select reqstyle =width:90%; onChange =getSelectedOptions(this)multiple> < option value =1> 1< / option> < option value =2> 2< / option> < option value =3> 3< / option>< / select>
I have a form with a select multiple. I want to get all selected values at onchange event but i dont know if this is possible. I think "this.value" only returns the last element selected.
Is it possible to get all the elements selected as array at onchange??
Thanks in advance.
<select name="myarray[]" id="myarray" class="select2-select req" style="width: 90%;" onChange="get_values(this.value)" multiple>
{foreach key=key item=value from=$myarray}
<option value="{$key}" >{$value}</option>
{/foreach}
</select>
解决方案
This example might help without jQuery:
function getSelectedOptions(sel) {
var opts = [],
opt;
var len = sel.options.length;
for (var i = 0; i < len; i++) {
opt = sel.options[i];
if (opt.selected) {
opts.push(opt);
alert(opt.value);
}
}
return opts;
}
<select name="myarray[]" id="myarray" class="select2-select req" style="width: 90%;" onChange="getSelectedOptions(this)" multiple>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
这篇关于Html select multiple获取onchange事件的所有值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文