Home  >  Article  >  Web Front-end  >  How to determine whether checkbox is selected in jquery

How to determine whether checkbox is selected in jquery

coldplay.xixi
coldplay.xixiOriginal
2020-12-24 09:41:383411browse

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.

How to determine whether checkbox is selected in jquery

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(&#39;width&#39;,&#39;300px&#39;);
            $("#v1").html("变短第一个文本框");
        }
        else{
            t1.css(&#39;width&#39;,&#39;169px&#39;);
            $("#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!

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