如何检查所有四个多重选择是否都选择了某个值? [英] How to check if all the four multiselect has some value selected?
问题描述
我有4种multiselect,都是通过这种方式动态创建的
I have 4 multiselect which are created dynamically by this fashion
var drp_nt = $('<select />', {
'id' : 'drp_' + nt,
'name' : 'drp_' + nt+'[]',
'multiple': true});
var drp_cnt = $('<select />', {
'id' : 'drp_' + cnt,
'name' : 'drp_' + cnt+'[]',
'multiple': true});
var drp_ctg = $('<select />', {
'id' : 'drp_' + ctg,
'name' : 'drp_' + ctg+'[]',
'multiple': true});
var drp_api = $('<select />', {
'id' : 'drp_' + api,
'name' : 'drp_' + api+'[]',
'multiple': true});
现在我要创建一个名为
function check_selection()
{
-----------------
-----------------
}
其中将包含用于检查所有multiselect是否选择了任何数据的代码.
which will contain the code to check whether all of the multiselect has any of the data selected.
我又如何从所有4个multiselect下拉菜单中使用and事件调用此函数(因为只要从任何multiselect中选择了任何选项,此函数都会被触发)
Also how can I call this function by using and event from all the 4 multiselect dropdowns(since this function will be triggered whenever any option is selected from any of the multiselect)
我尝试了多次选择的onchange事件,但没有调用该函数.
I tried the onchange event for multiselect, but it didn't call the function.
推荐答案
使用change event
选中所有选择框,或者不使用 filter()
use change event
to check all select box is selected or not using filter()
$("select").change(function () {
var selectedBox = $("select").filter(function () {
return this.value != "";
});
if (selectedBox.length == 4) {
alert("4 ckecked")
}
});
对于动态元素
$(document).on("change", "select[id^=drp_]", function(){
var selectedBox = $("select").filter(function () {
return this.value != "";
});
if (selectedBox.length == 4) {
alert("4 ckecked")
}
});
DEMO
这篇关于如何检查所有四个多重选择是否都选择了某个值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!