提交由 jquery 生成的表单
Submit a form generated by jquery
我的网站上的登录表单使用带有jquery-modal的覆盖/模式显示(http://kylefox.ca/jquery-modal/examples/)
我正在使用ajax + php来验证表单。如果验证通过,则应提交表单。
我可以停止提交以进行验证(使用返回 false),并且验证本身工作正常。但是我不知道如何提交表格
我尝试了许多幼稚的变体:返回true,$theform.submit(),$("body").unbind("#myloginform")等等。 但到目前为止没有运气
$("body").on("submit", "#myloginform", function() {
$theform = $(this);
$.ajax({
url: "login_check.php",
type: "POST",
cache: false,
timeout: 9000,
data: $theform.serialize(),
dataType: "json",
success: function(data) {
if (data) {
if (data.status == "ok") {
alert("success! now the form can be submitted");
// SUBMIT THE FORM (instead of the alert)
} else {
$("body #loginstatus").html(data.status);
}
} else {
alert("Error bla bla.");
}
},
error: function(e) {
alert("Error (ajax) bla bla.");
}
});
return false;
});
要提交 FORM,可以调用 js 原生提交方法:
document.getElementById('myloginform').submit();
查看变体:
$('#myloginform')[0].submit();
$('#myloginform').get(0).submit();
另一种方法是将 ajax 的选项设置为 this
context
:
$.ajax({
context: this,
...,
});
然后在成功回调中,使用以下方法提交表单:
this.submit();
编辑:我看到您已经在使用变量引用,因此在您的情况下,您也可以使用:
$theform[0].submit();
所有这些代码段都不会触发 jQuery 提交处理程序,从而避免循环引用错误。
另一种方法:
var checkValid = false;
$("body").on("submit", "#myloginform", function () {
$theform = $(this);
if (!checkValid) {
$.ajax({
url: "login_check.php",
type: "POST",
cache: false,
timeout: 9000,
data: $theform.serialize(),
dataType: "json",
success: function (data) {
if (data) {
if (data.status == "ok") {
alert("success! now the form can be submitted");
// Everything is OK
checkValid = true;
$theform.submit();// Next time, no validation process, just natural send of the form.
} else {
$("body #loginstatus").html(data.status);
}
} else {
alert("Error bla bla.");
}
},
error: function (e) {
alert("Error (ajax) bla bla.");
}
});
return false;
}
});
既然你使用的是jQuery,我建议你看看jQuery提交函数
http://api.jquery.com/submit/
相关文章:
- jQuery表单添加不适用于下拉列表
- 同时打印并提交表单-jQuery.Print
- 如何不清除表单jQuery中禁用的输入
- 如何检查输入中的值以及值是否存在提交表单?jquery.
- 登录表单 jQuery 插件无法按预期工作
- 谷歌电子表格网站表单jQuery脚本阻止发送
- 如何做不成功提交表单jquery
- 将表单和输入附加到表单jQuery Mobile
- 在我的切换表单jquery脚本中出现问题
- 使用动态表单 jquery 创建多维数组
- 使用一个类提交多个表单(jquery, rails)
- 有一个按钮打开右边的表单JQuery
- 取消按钮php表单jquery
- 按钮必须按一个特定的顺序发送表单- jquery
- 产品验证表单jquery
- rails嵌套表单jquery图像预览
- 获取表单(Jquery与Javascript)
- 将类型文件的输入附加到表单jquery中
- 如何从理想表单jQuery中删除验证规则
- Ruby on rails -加载更多按钮与kaminari和搜索表单.Jquery的问题