【发布时间】:2010-04-27 13:44:11
【问题描述】:
我有一个简单的表单,我希望以一种简单的方式回发到服务器并获得结果。我在后端使用自定义 ISAPI 插件,因此不能选择使用 JSON 或其他时髦的东西。我怎样才能做到最好?
编辑:如果可能,我也不想使用外部插件
【问题讨论】:
-
可惜了。否则我会为此建议使用非常易于使用的jQuery Form plugin。
我有一个简单的表单,我希望以一种简单的方式回发到服务器并获得结果。我在后端使用自定义 ISAPI 插件,因此不能选择使用 JSON 或其他时髦的东西。我怎样才能做到最好?
编辑:如果可能,我也不想使用外部插件
【问题讨论】:
使用serialize 获取表单的字符串表示形式,然后使用jQuery 的post AJAX 函数简单地发布它。
非常简单的示例(来自使用 PHP 的 jQuery 网站,但任何 URL 都可以):
$.post("test.php", $("#testform").serialize());
如果页面上有多个表单,您可以在按钮单击时使用此代码来获取正确的表单 ID(其中“someButton”可以是任何有效的 jQuery 选择器):
$('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());
});
【讨论】:
serialize()它的表单引用?
与 Marek 评论相同的另一种方法。
$.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
}
});
【讨论】: