admin管理员组

文章数量:1405349

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

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

Share Improve this question edited Sep 4, 2012 at 7:25 Danil Speransky 30.5k6 gold badges69 silver badges78 bronze badges asked Jul 28, 2012 at 8:52 Suave NtiSuave Nti 3,75713 gold badges56 silver badges78 bronze badges 5
  • Why not change instead of mousedown – Alex Ball Commented Jul 28, 2012 at 8:56
  • 1 Can you post the exact error message you receive? Even if the selector doesn't match anything, mousedown() should still be safe to call. – Frédéric Hamidi Commented Jul 28, 2012 at 8:56
  • @AlexBall : Even change is the same ..no luck – Suave Nti Commented Jul 28, 2012 at 8:58
  • 1 change name='isClubMember' to name=isClubMember – Slevin Commented Jul 28, 2012 at 9:00
  • @FrédéricHamidi : Have updated my qn with error. – Suave Nti Commented Jul 28, 2012 at 9:00
Add a ment  | 

4 Answers 4

Reset to default 7

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.

I guess the selector is broken in IE8 somehow, try [type='checkbox'], should be faster anyway

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

本文标签: javascriptJquery Checkbox change event not firingStack Overflow