2010-11-11 50 views
3

正如標題所說,我不能讓.attr('checked', false)在IE6上工作。我克隆了一些HTML,然後在將新克隆的HTML分配給元素之前,我運行它並取消了所有位於新克隆節中的複選框,這在除IE 6以外的所有瀏覽器中都可以正常工作。`attr('checked',false)`不適用於IE6

下面是代碼:

//give each input and select a custom id 
    clone.find('input, select').each(function(i) { 

      //get the id attribute 
      var id = $(this).attr('id'); 

      //uncheck all of the tick boxes 
      $(this).attr('checked', ''); 

      //get the name attribute 
      var name = $(this).attr('name'); 

      $(this).attr('name', name+"_"+count) 
      $(this).attr('id', id+"_"+count+"_"+i) 

    }); 

    //append the newly created area to the area wrapper 
    clone.appendTo('[data-custom="area_wrapper"]'); 

有什麼辦法,我可以解決這個問題呢?

+0

在你的代碼中你使用'attr('checked','')',這是否也不起作用? – 2010-11-11 11:03:34

回答

3

嘗試

.removeAttr("checked") 
+0

試試嗎?你不確定它會起作用嗎?難道沒有人真的知道jQuery的'attr()'和相關方法實際上應該做什麼? – 2010-11-11 11:38:22

+3

對不起。寵物尿道。 – 2010-11-11 11:38:54

+0

試過了,也不行。 – Odyss3us 2010-11-11 12:06:47

1

沒有HTML屬性「檢查=假」,只有「檢查=檢查」託運箱並沒有什麼非託運箱。

使用.removeAttr('checked');

7

最簡單的解決方案也是一個優化:

this.checked = false; 

事實上,你可以在此優化適用於所有代碼:

 //get the id attribute 
     var id = this.id; 

     //uncheck all of the tick boxes 
     this.checked = false; 

     //get the name attribute 
     var name = this.name; 

     this.name = name+"_"+count; 
     this.id = id+"_"+count+"_"+i; 

這是因爲底層jQuery代碼無論如何都會訪問這些屬性(attr主要直接使用屬性直到jQuery 1.6)。

更多的信息在http://whattheheadsaid.com/2010/10/utilizing-the-awesome-power-of-jquery-to-access-properties-of-an-element

+3

是是是。聽聽這個,大家。 – 2010-11-11 11:27:18

+0

感謝您的建議,將爲未來的項目記住它。現在就放棄它並回報。 – Odyss3us 2010-11-11 12:07:49

相關問題