現象:attr設置checked值顯示不正確
$("input[type=radio][value='']").attr("checked","checked");
$("input[type=radio][value='']").attr("checked",true);
兩者在html上的修改一致,但是頁面上的顯示沒有更新。
<input type="radio" name="reporttype" value="" checked="checked">

測試改為
$("input[type=radio][value='']").prop("checked",true);
相當於
$("input[type=radio]")[0].checked=true;
設置checked后html改為:

頁面顯示也正確。
原因:這里涉及兩個概念 特性(attribute) 和屬性(Property)。
attribute是dom節點自帶的屬性,如id,class,自定義放在html 標簽內的屬性(customattr)等,放在dom對象的attributes屬性中,這個屬性的類型是NamedNodeMap。
property是dom元素作為對象,其附加的內容,如childnodes,firstchild等,直接在對象上,部分attribute特性如id,checked,disabled等會被添加到元素對象上,作為dom屬性。
用.attr()操作 一般只更改了 attributes中的屬性值,如果這個屬性是dom元素的一個特性屬性的話,沒有改到對應的特性,特性是內部實現某些功能的參照,比如 radio 的checked 特性未改,顯示的選擇狀態也沒有及時更新到。

注:對於當前dom元素有的特性使用.prop()進行操作,而對於沒有對應特性的普通屬性使用.attr()操作就可以了。
留坑~~有空再查看jq源碼
