The attr method of the jQuery plug-in is often used to obtain the checked attribute value. The size of the obtained value is undefined. At this time, the prop method can be used to obtain its true value. The difference between the two methods is introduced below:
1. Obtain the checked attribute through the prop method. The returned checked value is boolean, and the selected value is true, otherwise it is false
<input type="checkbox" id="selectAll" onclick="checkAll()">全选function checkAll() {var checkedOfAll=$("#selectAll").prop("checked"); alert(checkedOfAll); $("input[name='procheck']").prop("checked", checkedOfAll); }
2. If the attr method is used to obtain it, if If the undefined checked attribute is initialized in the current input, $("#selectAll").attr("checked") will return undefined regardless of whether it is currently selected;
<span style="font-size: 16px"><input type="checkbox" id="selectAll" onclick="checkAll()" >全选</span> <br/><span style="font-size: 16px">如果当前input中初始化已定义checked属性,则不管是否选中,$("#selectAll").attr("checked")都会返回checked.</span><br/><br/>
<input type="checkbox" id="selectAll" onclick="checkAll()" checked>全选function checkAll() {var checkedOfAll=$("#selectAll").attr("checked"); alert(checkedOfAll); $("input[name='procheck']").attr("checked", checkedOfAll); }
If you use jquery, you should use the prop method to get and set the checked attribute, and you should not use attr.
The above is the detailed content of About the attr method and prop method in jQuery to obtain the checked attribute operation method of input. For more information, please follow other related articles on the PHP Chinese website!