我有两个下拉选择器的以下 HTML 代码:
<select class="sel1">
<option class="c21" selected></option>
<option class="c20"></option>
</select>
<select class="sel2">
<option class="c21" selected></option>
<option class="c22"></option>
</select>
如何检查两个选定的选项是否在不同的 <select>
中标签有相同的类吗?
$('.sel1').change(fucntion(){
var op1class = $('.sel1 option:selected').attr("class");
// How can I check if any other selected <select> tag has the same option class?
});
最佳答案
您在 ('.sel1 option:selected').attr("class");
$
$('.sel1').change(function(){
var op1class = $('.sel1 option:selected').attr("class");
var op2class = $('.sel2 option:selected').attr("class");
if(op1class == op2class ){
//you code
}
alert(op1class);
alert(op2class);
});
已更新
$('.sel1').change(function(){
var op1class = $('select option:selected').attr("class");
var arr = $('select option:selected').map(function(){
return $(this).attr("class");
}).get()
//var op2class = $('.sel2 option:selected').attr("class");
//if(op1class == op2class ){
//you code
//}
alert(arr);
//alert(op2class);
});
<强> Demo
关于javascript - 通过 jQuery 检查是否存在具有相同选项类的任何其他选择标签的更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30031827/