Home  >  Article  >  Web Front-end  >  jquery handles whether the checkbox check box is selected example code sharing

jquery handles whether the checkbox check box is selected example code sharing

小云云
小云云Original
2018-01-10 09:20:411361browse

This article mainly introduces the relevant information of jquery processing checkbox (checkbox) whether it is selected example code. Friends who need it can refer to it. I hope it can help everyone.

jquery handles whether the checkbox (checkbox) is selected

Now if a checkbox is selected, use checked=true, checked="checked" also Line

It would be better to use prop instead of attr. Although the attr() method can be used normally in versions before jQuery1.6, the prop() method must be used instead

Example code:


<!DOCTYPE html>
<html lang="en">
<head>

<meta charset="utf-8"/>
<title>checkbox</title>
</head>
<body>

<input type="button" id="btn1" value="全选">
<input type="button" id="btn2" value="取消全选">
<input type="button" id="btn3" value="选中所有奇数">
<input type="button" id="btn4" value="反选">
<input type="button" id="btn5" value="获得选中的所有值">

<input type="checkbox" value="checkbox1"/>
<input type="checkbox" value="checkbox2"/>
<input type="checkbox" value="checkbox3"/>
<input type="checkbox" value="checkbox4"/>
<input type="checkbox" value="checkbox5"/>

<script src="js/jquery-3.2.0.min.js"></script>
<script>
$(function(){

var checkbox = $("input[type=checkbox]");

$("#btn1").on("click",function(){
checkbox.prop("checked",true);
});

$("#btn2").on("click",function(){
checkbox.prop("checked",false);
});

$("#btn3").on("click",function(){
$("input[type=checkbox]:even").prop("checked",true);
});

$("#btn4").on("click",function(){
checkbox.each(function(){
if($(this).prop("checked")){
$(this).prop("checked",false);
}else{
$(this).prop("checked",true);
}
});
});

$("#btn5").on("click",function(){
var str = "";
$("input[type=checkbox]").each(function(){
if($(this).prop("checked")){
str += $(this).val() + ",";
}

});
console.log(str);
});

});
</script>


</body>
</html>

Related recommendations:

Determining whether the radio in the input box in html is selected

Summary jquery to determine whether a check box is selected example

jquery code to determine whether a single check box is selected_jquery

The above is the detailed content of jquery handles whether the checkbox check box is selected example code sharing. For more information, please follow other related articles on the PHP Chinese website!

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