开发者

Easy way to post form values using jQuery

I have a simple form that I wish to postback to the server in an easy fashion and get the result. I am using a custom ISAPI plugin on the backend so using JSON or other funky stuff isn't an option. How best can I do this?

Edit: I would also rather not use 开发者_Go百科external plugins, if possible


Use serialize to get a string representation of your form and then simply post it using jQuery's post AJAX function.

Very simple example (from jQuery's website using PHP, but any URL would do):

$.post("test.php", $("#testform").serialize());

If you have multiple forms on the page you can use this code on button click to get the correct form Id (where 'someButton' can be any valid jQuery selector):

$('someButton').click(function() {
    //old, less efficient code
    //var formId = $(this).closest("form").attr("id");
    //$.post("test.php", $("#" + formId).serialize());

    //as per Vincent Robert's suggestion, simplified version
    $.post("test.php", $(this).closest("form").serialize());
});


Another way to do the same thing as Marek commented.

$.ajax({
  type: 'POST',
  url: YOUR_URL,
  data: $('#YOURFORM').serialize(),
  success: function(data) {
    //Probably should do something that shows it worked.
  }
 error: function (xhr, ajaxOptions, thrownError){
    //Log the error if there was one
 }

});
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜