jQuery 停止表单提交
我刚刚整理了一个漂亮的表单,花了我几个小时,并进行了一些漂亮的 jQuery focusout 验证。 focusout 属性附加到 onload 的输入字段。然后,我通过触发 focusout 事件 onsubmit 来重新使用我的验证函数,但我不知道的是,如果我的任何验证函数失败,如何禁用表单提交。完整示例此处 (jsfiddle)
缩减示例:
addLoadEvent(AttachVal);
函数 AttachVal(){
$('input[name="FirstName"]').focusout(NameTest); $('input[name="LastName"]').focusout(NameTest); $('form[name="注册"]').submit(SubmitRegForm);
}
函数SubmitRegForm(){
$('input[name="FirstName"]').trigger('focusout'); $('input[name="LastName"]').trigger('focusout'); 警报(“提交”);
返回 假;
}
我尝试在 SubmitRegForm函数
function NameTest(){
var result = true;
var EnteredValue = $(this).val();
var alphaExp = /^[a-zA-Z ]+$/;
if (!(EnteredValue.match(alphaExp)))
{
var result = "Invalid";
}
if (result !== true)
{
$(this).parent().next().children('span.ErrorDescript').text(result);
}
else
{
$(this).parent().next().children('span.ErrorDescript').text("");
}
目的
中附加一个变量,如下所示:
var FNStat = $('input[name="FirstName"]').trigger('focusout');
是检查所有这些变量,如果它们都返回 true,则提交,但这只是返回一个对象 是否可以从该函数获取响应文本?...也许?或者是否有另一种方法可以强制停止当前在我的个人验证函数中运行的提交事件?
一如既往,任何建议都非常感激。
担
I've just put together a nice form which has taken me hours, with some natty jQuery focusout validation. The focusout attribute is attached to the input fields onload. I then re-use my validation functions by triggering the focusout event onsubmit, but what I don't know is how to disable the form submitting if any of my validation functions failed. Full example here (jsfiddle)
Cutdown example:
addLoadEvent(AttachVal);
function AttachVal(){
$('input[name="FirstName"]').focusout(NameTest); $('input[name="LastName"]').focusout(NameTest); $('form[name="Register"]').submit(SubmitRegForm);
}
function SubmitRegForm(){
$('input[name="FirstName"]').trigger('focusout'); $('input[name="LastName"]').trigger('focusout'); alert ("Submitting");
return
false;
}
Validation function:
function NameTest(){
var result = true;
var EnteredValue = $(this).val();
var alphaExp = /^[a-zA-Z ]+$/;
if (!(EnteredValue.match(alphaExp)))
{
var result = "Invalid";
}
if (result !== true)
{
$(this).parent().next().children('span.ErrorDescript').text(result);
}
else
{
$(this).parent().next().children('span.ErrorDescript').text("");
}
}
I tried attaching a variable in the SubmitRegForm function like this:
var FNStat = $('input[name="FirstName"]').trigger('focusout');
with the intention to check them all and submit if they all return true, but that just returns a object. Is it possible to get the response text from that function?...maybe? Or is there another way to force a halt of a Submit event that's currently running from within my individiual validation functions?
As always, any advice greatly appreciated.
Dan
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
捕获表单提交的正确方法是这样的:
e.preventDefault()
将阻止表单提交。The correct way to catch the form submission is like this:
The
e.preventDefault()
will stop the form from submitting.这是一个更新的小提琴。 我所做的是在 SubmitRegForm() 方法的返回中查看有多少勾选图像可见,如果为零,则继续提交表单。
Here's an updated fiddle. What I did was in the return of the SubmitRegForm() method just see how many tick images are visible and if it's zero go ahead and submit the form.