jQuery:如何检查是否有错误? [英] Jquery: How do I check if there are errors?

查看:105
本文介绍了jQuery:如何检查是否有错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用bassistance插件进行表单验证,现在我的表单由几个单选按钮组组成,其中一些是必需的".

I am trying to bassistance plugin for my form validation, now my form consists of several radio button groups, some of them are "required".

现在,

  1. 我如何确保所有这些广播电台都获得了价值?
  2. 一旦用户正确设置了它们,在提交之前,我应在哪里以及如何检查那些单选按钮的值?

推荐答案

您是否在问如何使用该插件?它有充分的文档证明,但是无论如何这些片段都应该提供帮助.在这里,我采用具有两个单选按钮组的简单形式,使用插件对其进行验证.验证代码中要注意的主要事情是,我们有两个处理程序回调(用于无效"和成功"状态)以及一组要应用的验证规则.

Are you asking how you can use the plugin? It's pretty well-documented, but anyway these snippets should assist. Here I take a simple form with two lots of radio-button groups, validate them using the plugin. The main things to note in the validation code are that we have two handler callbacks (for 'invalid' and 'success' states) plus a set of validation rules to apply.

首先,示例表格:

<form id="your-form">
    <fieldset>
        <p>
            <label for="group_one">Select an option (required)</label>
            <input type="radio" name="group_one" id="opt1-1">Option 1
            <input type="radio" name="group_one" id="opt1-2">Option 2
            <input type="radio" name="group_one" id="opt1-3">Option 3
        </p>
        <p>
            <label for="group_two">Select an option here too (required)</label>
            <input type="radio" name="group_two" id="opt2-1">Option 1
            <input type="radio" name="group_two" id="opt2-2">Option 2
            <input type="radio" name="group_two" id="opt2-3">Option 3
        </p>

        <p><input name="submit" type="submit" id="submit" value="Save" /></p>
    </fieldset>
</form>

…以及第二个验证码:

$(document).ready(function(){
    $("#your-form").validate({
        invalidHandler: function(form, validator){
            var errors = validator.numberOfInvalids();
            var message = (errors == 1) ? "One of your groups won't validate." : "Neither of your groups will validate.";
            alert(message);
        },
        submitHandler: function(){
            alert("Everything's OK, both radio button groups are set.");
            $('#your-form').ajaxSubmit()
        },
        rules: {group_one: {required: true}, group_two: {required: true}}
    });
});

有帮助吗?

这篇关于jQuery:如何检查是否有错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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