Php 点击后提交表格
通过jQuery进行以下表单验证:Php 点击后提交表格,php,jquery,Php,Jquery,通过jQuery进行以下表单验证: $("#Login").submit(function( event ) { var url = 'ajax/'; var data = {}; $("input").each(function() { data[$(this).attr('name')] = $(this).val(); }); $.post(url,data,function( resp ) { $("#formLo
$("#Login").submit(function( event ) {
var url = 'ajax/';
var data = {};
$("input").each(function() {
data[$(this).attr('name')] = $(this).val();
});
$.post(url,data,function( resp ) {
$("#formLoginErrorMessage").children().remove();
if(resp === " ")
{
console.log("Empty");
return; // The form should submit
} else if (typeof resp === "object") {
console.log(resp);
} else{
$("#formLoginErrorMessage").addClass("alert-danger");
$("#formLoginErrorMessage").append("<li>" + resp + "</li>");
console.log(resp);
}
},'json');
event.preventDefault();
});
$(“#登录”).submit(函数(事件){
var url='ajax/';
变量数据={};
$(“输入”)。每个(函数(){
数据[$(this.attr('name')]=$(this.val();
});
$.post(url、数据、函数(resp){
$(“#formLoginErrorMessage”).children().remove();
如果(resp==“”)
{
控制台日志(“空”);
return;//表单应该提交
}else if(响应类型==“对象”){
控制台日志(resp);
}否则{
$(“#formLoginErrorMessage”).addClass(“警报危险”);
$(“#formLoginErrorMessage”)。追加(“”+resp+” ”);
控制台日志(resp);
}
}“json”);
event.preventDefault();
});
此脚本正在检查一些错误,当resp为空时,表单应提交。但是如果选中了resp,则返回,似乎不会使表单提交。您可以在if
条件下调用该方法
$("#Login").submit(function (event) {
var form = this;
var url = 'ajax/';
var data = {};
$('input').each(function () {
data[$(this).attr('name')] = $(this).val();
});
$.post(url, data, function (resp) {
$("#formLoginErrorMessage").children().remove();
if (resp === " ") {
form.submit();
console.log("Empty");
return; // The form should submit
} else if (typeof resp === "object") {
console.log(resp);
} else {
$("#formLoginErrorMessage").addClass("alert-danger");
$("#formLoginErrorMessage").append("<li>" + resp + "</li>");
console.log(resp);
}
}, 'json');
event.preventDefault();
});
$(“#登录”).submit(函数(事件){
var form=此;
var url='ajax/';
变量数据={};
$('input')。每个(函数(){
数据[$(this.attr('name')]=$(this.val();
});
$.post(url、数据、函数(resp){
$(“#formLoginErrorMessage”).children().remove();
如果(resp==“”){
表单提交();
控制台日志(“空”);
return;//表单应该提交
}else if(响应类型==“对象”){
控制台日志(resp);
}否则{
$(“#formLoginErrorMessage”).addClass(“警报危险”);
$(“#formLoginErrorMessage”)。追加(“”+resp+” ”);
控制台日志(resp);
}
}“json”);
event.preventDefault();
});
您似乎对闭包感到困惑
$( "#Login" ).submit(function( event ) {
// main submit handling function starts here
...
$.post(url,data,function(resp){
// inner response handling function starts here
if(resp === " "){
console.log("Empty");
return; //This returns to jQuery internals somewhere inside $.post
} ...
},'json');
// stops form from submitting
event.preventDefault();
});
我添加了一些注释,以帮助您更好地了解范围
首先,您不应该依赖客户端验证。它可以无一例外地被超越。如果出于某种原因,您坚持使用这种方式,您可以使用一些外部范围变量来确定验证是否通过,但是您必须使用$form.submit()
而不是return
来实际提交表单。更改此.submit();形成。提交();它会起作用,并消除返回;那里没有必要。感谢@php\u nub\u qq