Home > Web Front-end > JS Tutorial > body text

About the attr method and prop method in jQuery to obtain the checked attribute operation method of input

一个新手
Release: 2017-09-29 10:15:31
Original
1540 people have browsed it

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=&#39;procheck&#39;]").prop("checked", checkedOfAll);
}
Copy after login

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/>
Copy after login


<input type="checkbox" 
id="selectAll" onclick="checkAll()" checked>全选function checkAll()
{var checkedOfAll=$("#selectAll").attr("checked");
alert(checkedOfAll);
$("input[name=&#39;procheck&#39;]").attr("checked", checkedOfAll);
}
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template