jQuery submit form without reloading page

前端 未结 3 1267
情歌与酒
情歌与酒 2020-12-06 11:52

I\'m not very familiar with jQuery. I\'m trying to make a form that is submitted in the background without reloading page.

I have a hidden div which shows and hides

相关标签:
3条回答
  • 2020-12-06 12:09

    Please check which jQuery version you are using because in after jQuery 1.8.2 "success" function from jQuery Ajax is deprecated.

    Use e.preventDefault() to prevent actual submit event.

    0 讨论(0)
  • 2020-12-06 12:12

    Rather than bind to the click event (input[type=submit]) you should bind to the submit event for the form.

    $("form").on("submit", function (e) {
        e.preventDefault();
    

    I'm also not sure about the validation. If it runs asynchronously, a callback is required. If it runs synchronously, when does it get triggered? It seems like it should be done when the form is submitted unless your validation plugin does that on its own:

    $("form").on("submit", function (e) {
        e.preventDefault();
        if ($(this).validate(options)) {
            $.ajax
    

    Using e.preventDefault will prevent reload and should allow your success div to display.

    0 讨论(0)
  • 2020-12-06 12:13

    1) Use event.preventDefault(); to keep the form from submitting -http://api.jquery.com/event.preventDefault/

    2) There isn't anything in the given script that should be causing #result to hide, but you can try $('#result').show() and see if that brings it back

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