search
Homephp教程PHP开发Analysis of checkbox selection problem based on jQuery

The example of this article analyzes the checkbox selection problem based on jQuery. Share it with everyone for your reference, the details are as follows:

I encountered a very strange problem recently when developing a project, that is, whether to select all or not select all in the checkbox
Using the jQuery framework. I have always used

//检测选中的checkbox
$('input[name="abc"]:checked').each(function(){})

However, I found that when I need to select all, I use

$('input[name="abc"]').attr('checked',true);
$('input[name="abc"]').attr('checked',false);

. It works when loading for the first time. Clicking it again will only show itself
But when I click When I checked the source code, I found that the checked attribute had been added. I was puzzled. Finally, I found out that it was the attr attribute. For checked, it would not change the dom style, but would only change its attribute value. jquery provided The alternative method is as follows

$('input[name="abc"]').prop('checked',true);
$('input[name="abc"]').prop('checked',false);

However, the problem comes again. When not selecting all, I can’t detect which element is clicked, and then I make a fuss about the name

$('input[name="abc[]:checked"').each(function(i){});
//或者
$('input[name="abc[]"').each(function(i){
  var flag = $(this).prop('checked');
  if(flag){
   //$(this) 即为选中元素
 }
})

problem solved.


Statement
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function