开发者

2 jQuery events on same action seem to cancel each other

开发者 https://www.devze.com 2023-01-16 06:49 出处:网络
**Update: I have pasted working code in order to erase any ambiguity about what is going on. I have also tried to remove the preventDefault on both handlers, does not help*

**Update: I have pasted working code in order to erase any ambiguity about what is going on. I have also tried to remove the preventDefault on both handlers, does not help*

I have a form where upon the button click, a JS event needs to happen, and the form needs to submit.

As per the code below, what I thought would happen is: alert(button), then alert(form), or vice versa. I do not care about sequence.

If i run it however, the alert(button) will show up, but the alert(form) will not.

If i comment out the code for the button, the form alert comes up.

Do i have some fundamental misunderstanding of how this is supposed to work?

jQuery(document).ready(function(){

  $("f开发者_运维百科orm.example").submit(function(event){
    event.preventDefault();
    alert("form submitted");
  });


  $("form.example button").click(function(event){
    event.preventDefault();
    alert("button clicked");
  });
)};


<form class="example" action="/v4test">
  <button type="submit">Meow!</button>  
</form>


After edit of OP

You do not need to preventDefault of the click.... only the submit... here is you working code:

jsFiddle example


jQuery(document).ready(function(){
    $('form.example').submit(function(event){
        event.preventDefault();
        alert("form submitted");
        // stop submission so we don't leave this page
    });
    $('form.example button').click(function() {
        alert("button clicked");
    });
});​

old answer

You can simply put your .click() and .submit() handlers in series, and they should not cancel out. You have some syntax errors in your pseudo code.... maybe those are causing problems?

Another potential problem is that $("form button") targets the HTML <button> tags. If you use <input type="button" /> you should use $("form:button") and note that <input type="submit" /> is not a button. Anyway, I'll assume you are in fact using the <button> tags.

Usually return false is used inside .submit(function() { ... });. This stops the form from being submited through HTML. s**[topPropagation][6]** is very different. It deals with stopping events "bubbling up" to the parents of elements....... But I don't see how this would effect your case.

If you are doing a true HTML submission, make sure to put your .click() handler first, since a true HTML submission will cause you to leave the page.

If you use return false inside .submit(), the form will not be submitted through the HTML, and you'll have to handle the submission with jQuery / Javascript / AJAX.

Anyway, here is a demonstration of both the .click() and .submit() events firing in series... the code is below:

jsFiddle Example


$(function() {

    $('form button').click(function() {

        // Do click button stuff here.

    });

    $('form').submit(function(){

        // Do for submission stuff here
        // ...
        // stop submission so we don't leave this page
        // Leave this line out, if you do want to leave
        // the page and submit the form, but then the results of your
        // click event will probably be hard for the user to see.
        return false;

    });

});​

The above will trigger both handlers with the following HTML:

<button type="submit">Submit</button>

As a note, I suppose you were using pseudo code, but even then, it's much easier to read, and one is sure you're not writing syntax errors, if you use:

$('form').submit(function() { /*submits form*/ }); 
$('form button').click(function() { /*does some action*/ });


If you put a return false on the click, it should cancel the default behavior. If you want to execute one then the other, call $('form').submit() within the click function. e.g.

$('form').submit { //submits form}

$('form button').click {
// does some action
$('form').submit();
}


There seems to be a bit of confusion about propagation here. Event propagation (which can be disabled by stopPropagation) means that events "bubble up" to parent elements; in this case, the click event would register on the form, because it is a parent of the submit button. But of course the submit handler on the form will not catch the click event.

What you are interested in is the default action, which in the case of clicking a submit button is to submit the form. The default action can be prevented by either calling preventDefault or returning false. You are probably doing the latter.

Note that in Javascript functions which do not end with an explicit return do still return a value, which is the result of the last command in the function. You should end your click handler with return; or return true;. I have no idea where I got that from. Javascript functions actually return undefined when there is no explicit return statement.


Does clicking the button submit the form? If so:

// Disable the submit action
$("form").submit(function(){
    return false; 
});

$("form button").click(function(){
    // Do some action here

    $("form").unbind("submit").submit();

});

If you don't unbind the submit event when you click the button, the submit will just do nothing.

0

精彩评论

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