开发者

Display an alert box when ticking checkbox in Jquery

开发者 https://www.devze.com 2023-04-03 06:43 出处:网络
How to display an alert box when ticking checkbox in Jquery? The alert box will disappea开发者_Go百科r when checkbox is unticked.

How to display an alert box when ticking checkbox in Jquery? The alert box will disappea开发者_Go百科r when checkbox is unticked.

I have tried the code below,but it doesn't work. Can anyone help me out? Thanks!

<script type="text/javascript">
        $(document).ready(function(){

            if($('#chkAll').is(':checked'))
           {
              alert("im checked");

           }

        });
        </script>
<input id="chkAll" />


try

$(document).ready(function(){
    $('#chkAll').change(function() {
        if($('#chkAll').is(':checked') {
            alert("checked");
        }
    });
});


Your input needs to be a checkbox, and to actually do something you will have to listen to a click or change event happening:

<script type="text/javascript">
  $(function(){
    $("#chkAll").bind("click",function(){
      if (this.checked) alert("I’m checked!");
    });
  });
</script>
<input type="checkbox" id="chkAll" />

Try this and see if it works!


Try this.

$(document).ready(function(){
        //$('#chkAll').click(getCheck());
        $('#chkAll').click(function() {
             if($('#chkAll').is(':checked'))
               {
                  alert("im checked");
               }
        });
    });


 <input type="checkbox" id="chkAll" />


You need to define either a change or a click function for it to run instead of what you have that runs after the page is loaded.

$(document).ready(function () {
    $('#chkAll').change(function () {
        alert( $(this).val () );
    });
});


The document read only looks at the state the checkbox is when the page is loaded. to trigger a function when the checkbox triggers a change event (when it is checked or unchecked) bind an event listener to it.

try using:

<script type="text/javascript">
$(document).ready(function(){

    $('#chkAll').bind('change', function () {
        if($('#chkAll').is(':checked'))
        {
            alert("im checked");
        }
    });

});
</script>
0

精彩评论

暂无评论...
验证码 换一张
取 消