Jquery attr('checked') returns checked or undefined to get the selected invalid_jquery
WBOY
Release: 2016-05-16 17:20:30
Original
923 people have browsed it
Suppose we now need a scenario like this: there is a checkbox on the page, and we expect to get whether it is selected through Jquery, or to let it be selected through Jquery. In versions before JQ1.6, we would write our code like this:
<script> <br>//Get whether it is selected<br>var isChecked = $('#cb' ).attr('checked'); <br><br>//Set checked<br>$('#cb').attr('checked',true); <br></script>
Writing like this was no problem before JQ1.6, but when we upgrade JQ1.6 to a higher version, problems arise. At this time we will find: $('#cb ').attr('checked'); What is returned is checked or undefined, not the original true and false. And the checked attribute has been initialized when the page is initialized and will not change with the change of status. So if the checkbox is selected at the beginning, then checked is returned. If it is not selected at the beginning, undefined is returned.
<script> <br>//Get whether it is selected<br>var isChecked = $('#cb'). prop('checked'); <br>//or <br>var isChecked = $('#cb').is(":checked"); <br>//Set checked<br>$('#cb ').prop('checked',true); <br></script>
analyzed the reasons, which can be understood like this:
It distinguishes between "attributes" and "characteristics". Attributes refer to "name, id", etc., and characteristics refer to "selectedIndex, tagName, nodeName" and so on. After JQ1.6, you can obtain attributes through the attr method and obtain characteristics through the prop method
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