简体   繁体   中英

Jquery Checkbox change event not firing

Am trying to sortout this small issue from past hour :

<script type="text/javascript">
$(document).ready(function(){        
    $("input[name='isClubMember']:checkbox").mousedown(function() {
         if (!$(this).is(':checked')) {
            this.checked = confirm("Are you sure?");
            $(this).trigger("change");
         }
    });
 });
</script>

Funnily this works in JSFiddle ..but not in my jsp page. Sample

The above code gives me null error at this line (in Debug console)

 $("input[name='isClubMember']:checkbox").mousedown(function() {

JQ -Ver : 1.7.2 Browser : IE8

Update : Error in IE console :

 'null' is null or not an object 

at the above mentioned line

It looks like your JSP page includes the Prototype library after jQuery.

Prototype's $() function takes an id and returns null if it cannot find the element, which is consistent with the behavior you're observing.

Try using jQuery instead of $ everywhere in your code:

jQuery("input[name='isClubMember']:checkbox").mousedown(function() {
    // ...
});

Or put your code in a closure that is passed the jQuery object in a $ argument:

(function($) {
    // The rest of your code...
})(jQuery);

Or take advantage of the fact that a ready handler is passed the same $ argument:

jQuery(document).ready(function($) {
    // The code in your 'ready' handler...
});

You may also want to run jQuery in noConflict mode, to avoid overriding Prototype's $() function if the order of inclusion changes.

i would suggest, you could add a class name to the checkbox, and find the checkbox by a class name! it seems thats the problem with ie8 in this case!

I think in your page jQuery is not connected. Check.

I think so because $ - function and returns object anyway, but if there is no jQuery then $(str) returns null.

我猜选择器在IE8中坏了,尝试[type ='checkbox'],无论如何应该更快

    $("input[name='isClubMember'][type='checkbox']").mousedown(function() {

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