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"
Step 1. Try a positive wave:
function selectAll(obj){ if(obj.checked) { $('input[name="xxx[]"]').attr("checked", true); } else { $('input[name="xxx[]"]').removeAttr("checked"); } }
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; } } }
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!