Home > Article > Web Front-end > How to determine whether checkbox is selected in jquery
Jquery method to determine whether the checkbox is selected: 1. Determine [$(checkbox id).prop("checked")] returns a boolean value type; 2. Determine [$(this).is (":checked")] also returns a boolean value.
The operating environment of this tutorial: windows7 system, jquery3.2.1 version, DELL G3 computer.
Recommended: jquery video tutorial
Jquery method to determine whether the checkbox is selected:
jQuery can greatly improve the html Writing efficiency, there are several checkbox selection judgments:
1, $(checkbox id).prop("checked")
returns a boolean value type
2, $(this).is(":checked")
The returned value is also a boolean value type
The following is an example of clicking the checkbox to modify the text box attributes:
<!doctype html> <html> <head> <meta charset="utf-8"> <title>exp8_3</title> </head> <body> <input type="text" name="first" id="first"><br> <input type="text" name="second" id="second"><br> <input type="checkbox" name="cb" id="hide" value="1"><span id="v0">隐藏第三个文本框</span><br> <input type="checkbox" name="cb" id="ml" value="2"><span id="v1">变长第一个文本框</span><br> <input type="text" name="third" id="third"> <script type="text/javascript" src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script> <script type="application/javascript"> var t1 = $("#first"); var t2 = $("#second"); var t3 = $("#third"); $(document).ready(function(e) { t2.mousedown(function(e) {//t2被鼠标按下后 var str = t1.val();//获得t1的文本信息 t2.val(str);//加载入t2的文本 }); $("#hide").click(function(e) { //var flag = $(this).is(":checked"); var flag = $(this).prop("checked"); t3.toggle();//动态显示隐藏文本框 if(flag) $("#v0").html("显示第三个文本框"); else $("#v0").html("隐藏第三个文本框"); }); $("#ml").click(function(e) { var flag2 = $(this).prop("checked"); if(flag2){ t1.css('width','300px'); $("#v1").html("变短第一个文本框"); } else{ t1.css('width','169px'); $("#v1").html("变长第一个文本框"); } }); }); </script> </body> </html>
Related free learning recommendations:javascript(Video)
The above is the detailed content of How to determine whether checkbox is selected in jquery. For more information, please follow other related articles on the PHP Chinese website!