0

我有一个页面,其中下拉列表是使用“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);
});
4

1 回答 1

0

你可以试试这个:

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而不是尝试遍历每个元素。

于 2015-10-09T16:01:17.687 回答