javascript - 通过 jQuery 检查是否存在具有相同选项类的任何其他选择标签的更改

标签 javascript jquery html

我有两个下拉选择器的以下 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/

相关文章:

javascript - 如何更新外部 javascript 文件中 html 对象的 css 属性

php - 检查 JSON 的响应是否为空

javascript - Knockout -> 绑定(bind)到嵌套结构,其中 Children 可以为 null

html - Bootstrap 标签没有包裹在 DIV 中?

javascript - 分配给数组无法按预期工作

javascript - 如何在 Nodejs 中正确从父目录导出和 require ?

javascript - 从 MySQL 数据库获取数据并添加到列表(NodeJS)

php - javascript php mysql float

javascript - Isotope jquery 插件在 chrome 上无法正确显示

html - Canvas contenteditable 不工作