How do I check whether a checkbox is checked in jQuery?

前端 未结 30 3426
花落未央
花落未央 2020-11-21 04:44

I need to check the checked property of a checkbox and perform an action based on the checked property using jQuery.

For example, if the age checkbox is

相关标签:
30条回答
  • 2020-11-21 05:02

    Use this:

    if ($('input[name="salary_in.Basic"]:checked').length > 0)
    

    The length is greater than zero if the checkbox is checked.

    0 讨论(0)
  • 2020-11-21 05:02

    The top answer didn't do it for me. This did though:

    <script type="text/javascript">
        $(document).ready(function(){
    
            $("#li_13").click(function(){
                if($("#agree").attr('checked')){
                    $("#saveForm").fadeIn();
                }
                else
                {
                    $("#saveForm").fadeOut();
                }
            });
        });
    </script>
    

    Basically when the element #li_13 is clicked, it checks if the element # agree (which is the checkbox) is checked by using the .attr('checked') function. If it is then fadeIn the #saveForm element, and if not fadeOut the saveForm element.

    0 讨论(0)
  • 2020-11-21 05:02

    I am using this:

     <input type="checkbox" id="isAgeSelected" value="1" /> <br/>
     <input type="textbox" id="txtAge" />
    
     $("#isAgeSelected").is(':checked') ? $("#txtAge").show() : $("#txtAge").hide();
    
    0 讨论(0)
  • 2020-11-21 05:04

    Use:

    <input type="checkbox" name="planned_checked" checked id="planned_checked"> Planned
    
    $("#planned_checked").change(function() {
        if($(this).prop('checked')) {
            alert("Checked Box Selected");
        } else {
            alert("Checked Box deselect");
        }
    });
    

        $("#planned_checked").change(function() {
            if($(this).prop('checked')) {
                alert("Checked Box Selected");
            } else {
                alert("Checked Box deselect");
            }
        });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <input type="checkbox" name="planned_checked" checked id="planned_checked"> Planned

    0 讨论(0)
  • 2020-11-21 05:08

    Use jQuery's is() function:

    if($("#isAgeSelected").is(':checked'))
        $("#txtAge").show();  // checked
    else
        $("#txtAge").hide();  // unchecked
    
    0 讨论(0)
  • 2020-11-21 05:08

    This works for me:

    /* isAgeSelected being id for checkbox */
    
    $("#isAgeSelected").click(function(){
      $(this).is(':checked') ? $("#txtAge").show() : $("#txtAge").hide();
    });
    
    0 讨论(0)
提交回复
热议问题