使用jQuery或纯JS获取多选框的价值 [英] Get value of multiselect box using jQuery or pure JS
本文介绍了使用jQuery或纯JS获取多选框的价值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在下面显示的代码中,如何使用jQuery或纯JavaScript在函数 val()
中获取多选框的值?
In the code shown below, how to get the values of multiselect box in function val()
using jQuery or pure JavaScript?
<script>
function val() {
//Get values of mutliselect drop down box
}
$(document).ready(function () {
var flag = 0;
$('#emp').change(function () {
var sub = $("OPTION:selected", this).val()
if (flag == 1) $('#new_row').remove();
$('#topics').val('');
var html = '<tr id="new_row" class="new_row"><td>Topics:</td><td> <select id="topic_l" name="topic_l" class="topic_l" multiple="multiple">';
var idarr = new Array();
var valarr = new Array(); { %
for top in dict.tops %
}
idarr.push('{{top.is}}');
valarr.push('{{pic.ele}}'); { % endfor %
}
for (var i = 0; i < idarr.length; i++) {
if (sub == idarr[i]) {
html += '<option value="' + idarr[i] + '" >' + valarr[i] + '</option>';
}
}
html += '</select></p></td></tr>';
$('#tops').append(html);
flag = 1;
});
});
</script>
Emp:
<select id="emp" name="emp">
<option value=""></option>
<option value="1">1</option>
</select>
<div name="tops" id="tops"></div>
<input type="submit" value="Create Template" id="create" onclick="javascript:var ret=val();return ret;">
提前致谢!
推荐答案
从 select
调用的 val
函数将返回一个数组,如果它是一个数组。 $('select#my_multiselect')。val()
将返回所选选项的值数组 - 您不需要循环并自己获取它们。
the val
function called from the select
will return an array if its a multiple. $('select#my_multiselect').val()
will return an array of the values for the selected options - you dont need to loop through and get them yourself.
这篇关于使用jQuery或纯JS获取多选框的价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文