2012-01-05 46 views
2

我想實現一個簡單的檢查所有選項。點擊複選框後,頁面中的所有複選框應該被選中。首先嚐試檢查主複選框的值無法操縱IE8中的複選框使用jquery

下面的代碼在Firefox中正常工作,但在IE8中無法正常工作。如何解決這個問題

function checkAll() 
    { 

     alert("value of checkbox = "+ $('#mastercheckbox').attr('checked')) 


    } 

和HTML代碼

 <input type="checkbox" id="mastercheckbox" onchange="checkAll();"/> 

回答

1

查找到prop()http://api.jquery.com/prop/

該文檔提供了更好的方式多以檢查「檢查」複選框。

elem.checked      | true (Boolean) Will change with checkbox state 
$(elem).prop("checked")   | true (Boolean) Will change with checkbox state 
elem.getAttribute("checked")  | "checked" (String) Initial state of the checkbox; does not change 
$(elem).attr("checked")(1.6)  | "checked" (String) Initial state of the checkbox; does not change 
$(elem).attr("checked")(1.6.1+) | "checked" (String) Will change with checkbox state 
$(elem).attr("checked")(pre-1.6) | true (Boolean) Changed with checkbox state 
+0

忘了那一個。感謝您的提醒 – mplungjan 2012-01-05 06:30:02