Check if checkbox is NOT checked on click - jQuery

后端 未结 8 384
心在旅途
心在旅途 2021-01-30 06:02

I want to check if a checkbox just got unchecked, when a user clicks on it. The reason for this is because i want to do a validation when a user unchecks a checkbox. Because atl

相关标签:
8条回答
  • 2021-01-30 06:38

    Check out some of the answers to this question - I think it might apply to yours:

    how to run click function after default behaviour of a element

    I think you're running into an inconsistency in the browser implementation of the onclick function. Some choose to toggle the checkbox before the event is fired and some after.

    0 讨论(0)
  • 2021-01-30 06:41

    Do it like this

    if (typeof $(this).attr("checked") == "undefined" )
    
    // To check if checkbox is checked
    if( $(this).attr("checked")=="checked")
    
    0 讨论(0)
  • 2021-01-30 06:43
    <script type="text/javascript">
    
     if(jQuery('input[id=input_id]').is(':checked')){
      // Your Statment
     }else{
      // Your Statment
     }
    
     OR
    
     if(jQuery('input[name=input_name]').is(':checked')){
      // Your Statment
     }else{
      // Your Statment
     }
    
    </script>
    

    Code taken from here : http://chandreshrana.blogspot.in/2015/10/how-to-check-if-checkbox-is-checked-or.html

    0 讨论(0)
  • 2021-01-30 06:50

    Try this:

    if(!$(this).is(':checked'))
    

    demo

    0 讨论(0)
  • 2021-01-30 06:50
    $(document).ready(function() {
            $("#check1").click(function() {
                var checked = $(this).is(':checked');
                if (checked) {
                    alert('checked');
                } else {
                    alert('unchecked');
                }
            });
        });
    
    0 讨论(0)
  • 2021-01-30 06:50

    The Answer already posted .But We can use the jquery in this way also

    demo

    $(function(){
        $('#check1').click(function() {
            if($('#check1').attr('checked'))
                alert('checked');
            else
                alert('unchecked');
        });
    
        $('#check2').click(function() {
            if(!$('#check2').attr('checked'))
                alert('unchecked');
            else
                alert('checked');
        });
    });
    
    0 讨论(0)
提交回复
热议问题