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

JQuery determines whether the checkbox is selected and other check box operation methods collection_jquery

WBOY
Release: 2016-05-16 15:57:11
Original
1378 people have browsed it

1. jquery determines whether the checkbox is selected and changes the checkbox status

Three ways to determine checked in jquery:

Copy code The code is as follows:

.attr('checked): //Look at version 1.6, return: "checked" or "undefined"; 1.5-return: true or false
.prop('checked'): //16 :true/false
.is(':checked'): //All versions: true/false//Don’t forget the colon

Several ways to write jquery assignment checked:
All jquery versions can assign values ​​like this:
Copy code The code is as follows:

// $("#cb1").attr("checked","checked");
// $("#cb1").attr("checked",true);

jquery1.6: 4 types of assignment of prop:
Copy code The code is as follows:

// $("#cb1″).prop("checked",true);//It’s so simple that I won’t go into details
// $("#cb1″).prop({checked:true}); //map key-value pair
// $("#cb1″).prop("checked",function(){
return true;//The function returns true or false
});
//Remember there is this kind of thing:$("#cb1″).prop("checked","checked");

2. How does jquery determine whether a checkbox is selected

Everyone knows that in html if a checkbox is checked it is checked="checked".

But if we use jquery alert($("#id").attr("checked")), it will prompt you that it is true instead of checked

So many friends judge that if($("#id").attr("checked")=="true") is wrong. In fact, it should be if($("#id").attr(" checked")==true)
The example includes several functions.

Copy code The code is as follows:







Code
Copy code The code is as follows:



 
  New Document
 
   
 
 
 
 

  
  
  
  
  
  

  
   checkbox1
  
   checkbox2
  
   checkbox3
  
   checkbox4
  
   checkbox5
  
   checkbox6
  
   checkbox7
  
 checkbox8
 

 


三、 jquery判断checkbox是否被选中

在html的checkbox里,选中的话会有属性checked="checked"。

如果用一个checkbox被选中,alert这个checkbox的属性"checked"的值alert($"#xxx".attr("checked")),会打印出"true"",而不是checked"!
如果没被选中,打印出的是"undefined"。

不要尝试去做这样的判断:if($"#xxx".attr("checked")=="true")或者if($"#xxx".attr("checked")=='checked')
应该是if($("#checkbox1").attr("checked")==true)
全选和全不选函数

复制代码 代码如下:

function checkAll(){
if($("#checkbox1").attr("checked")==true){
$("input[name='xh']").each(function() {
$(this).attr('checked',true);
});
}else {
$("input[name='xh']").each(function() {
$(this).attr('checked',false);
});
}
}

4. JQuery determines whether the checkbox is selected, selects all checkboxes, and obtains the checkbox selected value
JQuery is a very easy to use framework, but there are many things that we need to learn in depth.

There are many ways to judge whether the checkbox is selected online. Here is one method. Personally I think

More convenient.

Because it is relatively simple and has no technical content, just code it directly

Copy code The code is as follows:

http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >
http://www.w3.org/1999/xhtml">


JQuery determines whether the checkbox is selected, selects all checkboxes, and obtains the selected value of the checkbox



























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