【问题标题】:jQuery Check SelectedIndex For Each Dropdown On Page By ClassjQuery 按类检查页面上每个下拉列表的 SelectedIndex
【发布时间】:2015-10-09 15:43:37
【问题描述】:

我有一个使用“issueDropDown”类动态创建下拉列表的页面。我正在尝试使用“issueDropDown”类检查所有下拉列表的页面,以查看它们是否已更改。基本上,如果每个下拉列表的 selectedIndex (禁用)仍然设置,我执行一些操作。

例子:

<select class='issueDropDown'>
    <option selected disabled>Select Type</option>
    <option value="1">Type 1</option>
    <option value="2">Type 2</option>
</select>

到目前为止我所拥有的不起作用(不检查每个):

$('body').on('change', '.issueDropDown', function() {
    $('.issueDropDown').change(function(){
        var selIndex = $(".issueDropDown").filter(function(){
       return this.selectedIndex > 0;
    }).length;

    // I'll execute something here once I check that it's still "Select Type"
    alert('test: ' + selIndex);
});

【问题讨论】:

    标签: jquery dynamic each selectedindex dropdown


    【解决方案1】:

    你可以试试这个:

    var selIndex;
    $('body > .issueDropDown').change(function(){
              selIndex = $(".issueDropDown").filter(function(){
              return this.selectedIndex > 0;
            });
    
            // If selIndex is defined, do something.
            if(selIndex) {
    
            } else {
               // selIndex is not defined, so do something else.
            }
    
    });
    

    在您的代码中,selIndex 也超出了范围。

    编辑:如果您使用 jQuery 动态更改这些元素,您还可以查看 MutationObserver 而不是尝试遍历每个元素。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-06-04
      • 2011-10-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多