使用jQuery发布表单值的简单方法 [英] Easy way to post form values using jQuery

查看:109
本文介绍了使用jQuery发布表单值的简单方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的表单,我希望以简单的方式回发到服务器并获得结果。我在后端使用自定义ISAPI插件,因此使用JSON或其他时髦的东西不是一种选择。如何才能做到这一点?



编辑:如果可能的话,我宁愿不使用外部插件

解决方案

使用 serialize 来获取表单的字符串表示,然后使用jQuery的

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

如果您在页面上有多个表单,您可以在按钮上单击此代码以获取正确的表单Id(其中'someButton'可以是任何有效的jQuery选择器):

  $('someButton')。click(function() {
//旧,效率较低的代码
// var formId = $(this).closest(form)。attr(id);
// $。post test.php,$(#+ formId).serialize());

//根据Vincent Robert的建议,简化版本
$ .post(test.php ,$(this).closest(form)。serialize());
});


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

解决方案

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());
});

这篇关于使用jQuery发布表单值的简单方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆