2015-10-09 22 views
0

我有一個頁面,下拉動態創建類「issueDropDown」。我試圖檢查頁面中的所有下拉類與「issueDropDown」,以查看他們是否被更改或不。基本上,如果每個下拉列表(其被禁用)的selectedIndex仍然設置,我執行一些操作。jQuery Check 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); 
}); 

回答

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而不是試圖遍歷每個元素。

相關問題