Regarding the problem of JQuery full selection/inverse selection failing for the second time

一个新手
Release: 2017-10-12 10:28:22
Original
1248 people have browsed it

Recently, I encountered a problem in the project. When testing the select/unselect function, when the parent box is selected/unselected for the first time, the all-selected/unselected state of the sub-box can be synchronized, and then the parent box can be clicked. , the subframe becomes unresponsive. The general structure of the original code is as follows:


'input[name="xxx[]"]').attr("checked"
Copy after login

Step 1. Try a positive wave:


function selectAll(obj){ if(obj.checked) { $('input[name="xxx[]"]').attr("checked", true); } else { $('input[name="xxx[]"]').removeAttr("checked"); } }
Copy after login

Pawn-----has no effect at all, discard it.

Step 2. After a quick search on the Internet, I found that this problem is relatively common. Among people who have encountered this problem, I should be thousands of miles away. I clicked on a few and looked at them. Basically, they said that using prop instead of attr can solve the problem. The solution is as follows:

However, the version used in the project is lower than 1.6 and I was told that the latest version is lower than 1.6. It is best not to change the version and discard it.

Step 3. I have no choice but to abandon JQuery... Try to use native js writing method. The code is as follows:


function selectAll(obj){ var xxx = document.getElementsByName("xxx[]"); if(obj.checked) { for(var i = 0;i < xxx.length;i++) { xxx[i].checked = true; } } else { for(var i = 0;i < xxx.length;i++) { xxx[i].checked = false; } } }
Copy after login

Test It was solved successfully. In fact, it is a small problem, but it gave me some inspiration. I cannot be limited to one box. If I think about the problem from another angle, I can often solve the problem better.

The above is the detailed content of Regarding the problem of JQuery full selection/inverse selection failing for the second time. 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 Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!