2012-11-08 65 views
2

我有兩個下拉,一個影響另一個。第一個作爲第二個過濾器。隱藏(display:none)選項不適用於IE8。所以我需要實際刪除選項。但我希望能夠將選項重置爲原始列表,併爲其他團隊再次篩選它們。問題是我刪除選項,永遠不會讓他們回來,或者我從不刪除任何東西。我的直覺告訴我,它與引用或將新對象分配回DOM相關。如何刪除選項並保留原件的副本?

1st dropdown(dd1) - 這個html在我的代碼中是正確的,我很難顯示它。

<select id="pTeamFilter" onchange="teamChanged();" name="pTeamFilter"> 
    <option value="0">select a Team</option> 
    <option value="4"> Property Team </option> 
    <option value="7"> Rick's Team </option> 
    <option value="10"> Robert's Team </option> 
</select> 

第二下拉(DD2)

<select id="pAnalystFilter" name="pAnalystFilter"> 
    <option value="0">select an Analyst</option> 
    <option data-teamid="7" value="682"> Clark Kent </option> 
    <option data-teamid="10" value="652"> Bruce Wayne </option> 
    <option data-teamid="10" value="7"> Peter Parker </option> 
    <option data-teamid="13" value="971"> Bruce Banner </option> 
</select> 

JS/JQ:

var analystFullArrayElement; 
jQuery(document).ready(function() { 
    analystFullArrayElement = jQuery(document.getElementById('pAnalystFilter')).clone(); 
}); 

function teamChanged() { 
    //get the team id 
    var teamId = document.getElementById('pTeamFilter').value; 
    //get full list of Analysts. 
    var newAnalystElement = jQuery(analystFullArrayElement).clone(); 
    jQuery('option', newAnalystElement).each(function() { 
     //remove unwanted analysts 
     if ((jQuery(this).attr("data-teamid") != teamId) && (jQuery(this).val() != 0)) { 
      jQuery(this).remove(); 
     } 
    }); 
    document.getElementById('pAnalystFilter').innerHTML() = jQuery(newAnalystElement).html(); 
    //var analystElement = document.getElementById('pAnalystFilter'); 
    //jQuery(analystElement).val(0); 
} 
+0

得數在右側列出的相關問題,我現在已經在IE8在XP一個可行的解決方案。 –

回答

1

此外,您可以:

  1. 使用jQuery事件結合,而不是 「的onchange」 屬性。
  2. 使用$()而不是jQuery。如果你使用noConflict,你可以做到這一點

    (函數($){ 你在這裏與$代碼,而不是jQuery的 })(jQuery的);

  3. 使用$(document).ready(function() {})$(function() {})代替

  4. 使用鏈接。我的意思是

    jQuery('#pAnalystFilter')。empty()。append(jQuery(newAnalystElement).html());

  5. 使用數據()方法來獲得ATTR(「數據水木清華」)

  6. 如果使用$(這)一個以上的時間,最好將其存儲在一個變量

GT(0)中選擇,比應用濾波器,所以

$('option', newAnalystElement).each(function(){ 
    var $this = $(this); 
    if(($this.data("teamid") != teamId) && ($this.val() != 0)){ 
     $this.remove(); 
    } 
}); 
(function ($) { 
    $(function() { 

     var analystFullArrayElement; // by the way, your global scope is clean and it is good 

     $(function() { 
      analystFullArrayElement = $('#pAnalystFilter').clone(); 
     }); 

     $("#pTeamFilter").change(function() { 
      //get the team id 
      var teamId = $('#pTeamFilter').val(); 
      //get full list of Analysts. 
      var newAnalystElement = $(analystFullArrayElement).clone(); 
      $('option', newAnalystElement).each(function(){ 
       //remove unwanted analysts 
       var $this = $(this); 
       if(($this.data("teamid") != teamId) && ($this.val() != 0)){ 
        $this.remove(); 
       } 
      });     
      $('#pAnalystFilter').empty().append($(newAnalystElement).html()); 
     }); 

    }); 
})(jQuery);

另外yoг可以通過添加避免比較

變成

$('option:gt(0)', newAnalystElement).filter(function() { 
    return $(this).data("teamid") != teamId; 
}).remove(); 
0

兩個變化。 1.我將document.getElements更改爲jQuery調用。

  analystFullArrayElement = jQuery('#pAnalystFilter').clone(); 
  1. 我用了一個.empty(),接着是.append(),以擺脫舊名單,並應用新的名單。

    function teamChanged(){ 
         //get the team id 
         var teamId = jQuery('#pTeamFilter').val(); 
         //get full list of Analysts. 
         var newAnalystElement = jQuery(analystFullArrayElement).clone(); 
         alert(jQuery(newAnalystElement).html()); 
         jQuery('option', newAnalystElement).each(function(){ 
          //remove unwanted analysts 
          if((jQuery(this).attr("data-teamid") != teamId) && (jQuery(this).val() != 0)){ 
           jQuery(this).remove(); 
          } 
         }); 
         alert(jQuery(newAnalystElement).html());     
         jQuery('#pAnalystFilter').empty(); 
         jQuery('#pAnalystFilter').append(jQuery(newAnalystElement).html()); 
        } 
    
相關問題