开发者

jquery: firefox cant find id tag but safari can?

开发者 https://www.devze.com 2022-12-13 05:02 出处:网络
this problem is so weird. i have used jquery to link a specific id to an alert(\"hello\"). $(\"#submit_reply\").live(\"click\", function() {

this problem is so weird.

i have used jquery to link a specific id to an alert("hello").

$("#submit_reply").live("click", function() {
    event.preventDefault();
    alert("hellooooo");
});

now in safari when i click it works. but when im using firefox its not working. the submit_reply (a submit button) refreshes the whole page. that is to say, jquery wasnt able to bind it.

how can i debug this problem? firefox shows nothing wron开发者_如何学Gog in console. but why did it work in safari? what tools can i use to check what the problem is...help!


You are not passing in the event object to the function.

Try this:

$( "#submit_reply" ).live( 'click', function( e ) {
     e.preventDefault();
     alert( 'hello' );
} );

Or this:

$( "#submit_reply" ).live( 'click', function( ) {
     alert( 'hello' );
     return false;
} );

As long as e.preventDefault() or return false; are executed before the end of the function, you're good to go.


The problem is probably that you never send in the event parameter into the function, so the first line in your click handler fails and Firefox resorts to the original click action - follow the link.

Try this change:

$('#submit_reply').live('click', function(ev) {
    ev.preventDefault();
    alert('hello, is there anyone home?');
});
0

精彩评论

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