jQuery - Form still submits with return false

后端 未结 2 1375
醉话见心
醉话见心 2021-01-25 09:06

http://jsfiddle.net/QsTfc/

For some odd reason the form keeps submitting even though I am returning false. I have tried:

$(\"#submitEdit\").submit(functi         


        
相关标签:
2条回答
  • 2021-01-25 09:36

    According to your jsfiddle demo your form is added dinamically. So, you have to use delegation:

    $(document).on("submit", "#submitEdit", function() {
        var url = $(this).attr('action');
        var data = $(this).serialize();
        alert(data);
        $.post(url, data, function(returned) {
            $(parentId + "Field").html(returned);
            return false;
        });
        return false;
    });
    

    JSFIDDLE

    0 讨论(0)
  • 2021-01-25 09:44

    you need to put your event parameter in your call back function

    $("#submitEdit).submit(function(event) {
        event.preventDefault();
    });
    

    and try this:

    $(document).on("submit", "#submitEdit", function(event) {
        event.preventDefault();
        var url = $(this).attr('action');
        var data = $(this).serialize();
        alert(data);
        $.post(url, data, function(returned) {
            $(parentId + "Field").html(returned);
            return false;
        });
    });
    

    when to use return false and when to use event.preventDefault(). you can take a look for this question: When should I use return false in jquery function?

    0 讨论(0)
提交回复
热议问题