简体   繁体   中英

jquery checkbox change,click event not working with jQuery1.8.2

I have a simple js where i am showing an alert on click or change event of checkbox. I am using jquery-1.8.2.js library file. The code is working fine in jsFiddle but its not working elsewhere. I am unable to figure out what could be the issue here.

<input type="checkbox" id-="chkJS"/><label for "chkJS">click this</label>

$(document).ready(function(){
    $('#chkJS').change(function(){
    alert("changed it");
    });
    $('#chkJS').click(function(){
    alert("clicked it");
    });
});

You forget = in label declaration and id-="chkJS" for input type.

<input type="checkbox" id="chkJS"/><label for="chkJS">click this</label>

OR

Your code working fine in fiddle because fiddle load jQuery automatically for you and at other you might not load jQuery.

So please check if jQuery loaded or not.

you have a typo in your input element of id-="chkJS" attribute. it should be like this

<input type="checkbox" id="chkJS"/><label for="chkJS">click this</label>

Demo : CheckBox demo

id-="chkJS"

应该

id="chkJS"

You code the element was having an hyphen for the id attribute, like id-=, so when the selector searches for the element there is no id for that element. Please change it as follows,

<script src="https://www.google.com/jsapi"></script>
<script>
google.load('jquery', '1.3.1');
</script>
<body>
      <input type="checkbox" id="chkJS"/><label for "chkJS">click this</label>
        <script>
        $(document).ready(function(){
            $('#chkJS').change(function(){
            alert("changed it");
            });
            $('#chkJS').click(function(){
            alert("clicked it");
            });
        });
        </script>
</body>

I hope it helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM