I have two com开发者_运维技巧bo box named as combo1 and combo2.combo1 having multiple selection and another combo2 having single select value.i want to check that the combo2 select value belongs to combo1. multi selected value that means combo1 having values are {1,2,3,4} and combo2 select value is 4, otherwise error alert message.how it will be implemented using Java script.
Thanks
My solution stores the selected values of "combo1" as properties of an object which can then be queried to see if it has a property named by the selected value of "combo2":
function combo1includes2(c1, c2) {
// Store multi-select values in "values".
var vs={}, opts=c1.options;
for (var i=0; i<opts.length; i++) {
if (opts[i].selected) {
vs[opts[i].value] = true;
}
}
return vs.hasOwnProperty(c2.value);
}
// Example usage.
var combo1 = document.form1.combo1
, combo2 = document.form1.combo2;
alert(combo1includes2(combo1, combo2));
Here is a working example - http://jsfiddle.net/maerics/fpDkV/7/
精彩评论