本人使用的版本是:jquery-1.10.2
最簡單的“全選/取消全選”的例子:
//全選/取消全選 function selectAll(pid){ var checkVal = $("#"+pid).prop("checked"); alert(checkVal); $("#"+pid+"_child :checkbox").each(function(){ $(this).prop("checked", checkVal); }); //以前我的寫法 // if("checked" == checkVal || true == checkVal) { // $("#"+pid+"_child :checkbox").each(function(){ // $(this).removeAttr("checked"); // }); // } else { // $("#"+pid+"_child :checkbox").each(function(){ // $(this).attr("checked", "checked"); // }); // } }
發現attr方法獲取的checked屬性一直是:undefined,無論怎麼設置都是undefined,無奈最後找到prop代替attr了,而prop的返回結果只可能是true和false,這樣一來設置checked值就更加便捷了……
未完待續……