如何火jQuery的功能只有当表单有效 [英] How to fire jQuery function only if form is valid

查看:133
本文介绍了如何火jQuery的功能只有当表单有效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个jQuery函数绑定到我的提交按钮这样的:

I have a jQuery function tied to my submit button like this:

$(function () {
    $('#signupform').submit(function () {
        alert('test');
    });
});

然而,它触发的形式是否是有效的。我的模型装饰有各种DataAnnotations和客户端验证工作良好,但我只想要一个jQuery函数解雇如果表单已经被验证。我该如何实现这个目标?

However, it fires whether or not the form is valid. My model is decorated with various DataAnnotations and the client-side validation is working well, but I only want that jQuery function to fire if the form has been validated. How do I accomplish that?

编辑::要澄清一下,我使用MVC DataAnnotations + jQuery的不显眼的JavaScript来处理客户端验证。我没有我自己的JavaScript验证程序编写。内置的jQuery验证是做验证表单的一个伟大的工作,但我只需要知道如何得到验证的结果变成我自己的函数中的一个布尔值。

To clarify, I'm using MVC DataAnnotations + jQuery's unobtrusive javascript to handle the client-side validation. I do not have my own javascript validation routines written. The built in jQuery validation is doing a great job of validating the form, but I just need to know how to get the results of that validation into a boolean value within my own function.

推荐答案

如果您正在使用JQuery验证不显眼的验证你可以:

If you are using jquery validate unobtrusive validation you could:

$(function () {
    $('#signupform').submit(function () {
        if($(this).valid()) {
            alert('the form is valid');
        }
    });
});

这篇关于如何火jQuery的功能只有当表单有效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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