开发者

What's the easiest way to post this form using jQuery? [closed]

开发者 https://www.devze.com 2023-04-12 11:06 出处:网络
Closed. This question is opinion-based. It is not currently accepting answers. Want to improve this question? Update the question so it can be answered with facts and citations by editing th
Closed. This question is opinion-based. It is not currently accepting answers.

Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.

Closed 8 years ago.

Improve this question

OK, so I thought I had this licked by I was wrong.

Basically, I have a form wrapped in a span (#container) and embedded in a page. The server side does all the validation and detects whether it's a full HTTP request or XHR.

If it's a full HTTP request, the server return开发者_如何学Gos a full page, complete with layout. If it's an XHR request, the server returns only the form element and its children. The form may or may not include errors.

So, I'm looking for the easiest way to ajaxify this form using jQuery. Basically, I think I need to do something like this:

  • When the document is ready, bind a function to the click event of the submit button using jQuery's .live() method. (This is easy enough).

The function needs to do something like this:

  • Trigger the submit event on the form
  • Capture the html response
  • Replace the contents of #container with the response contents

Does this sound about right? Any asssitance is appreciated!


If you trigger the submit event on the form, it will submit as a full http request unless you add onsubmit = "return false;" to your form tag. The easiest way would be to use $.post() in combination with a $.serialize() where you can also capture the servers response. It might look something like this.

$.post(
    'myFormURL.php',
    $('#idOfMyForm').serialize(),
    function(results) {
        $('#container').html(results);
    }
);
0

精彩评论

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

关注公众号