Easy way to post form values using jQuery

2019-08-03 23:52发布

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 external plugins, if possible

2条回答
爷、活的狠高调
2楼-- · 2019-08-04 00:28

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
 }

});
查看更多
来,给爷笑一个
3楼-- · 2019-08-04 00:32

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());
});
查看更多
登录 后发表回答