jQuery复选框验证 [英] jQuery checkbox validation

查看:181
本文介绍了jQuery复选框验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用下面的jQuery代码来验证表单。现在这一切都很好,但我想要复选框验证使用 .validator 而不是其当前的警告但是当我在复选框的输入框中添加 required =required时,我会收到所有复选框的邮件,而我只需要一个。

I'm using the jQuery code below to validate a form. Now it all works great but I would like for the checkbox validation to use the .validator rather than its current alert, but when I add required="required" to the checkbox input boxes, I get messages for all of the checkboxes, whereas I only need one.

$("#request-form").validator({
    message: '<div><em/></div>',
    position: 'top left',
    offset: [3, 40]
});

$("#see").overlay({mask: '#999', fixed: false}).bind("onBeforeClose", function(e) {
    $(".error").hide();
});

$('#request-form').submit(function(){
    if(!$('#request-form input[type="checkbox"]').is(':checked')){
        alert("Please check at least one.");
        return false;
    }
});

希望这有意义。提前致谢。 S

Hope this makes sense. Thanks in advance. S

推荐答案

这里有一些代码将使用 JQuery Validation Plugin 。我不知道这是否会与你使用的,因为我从来没有听说过。

Here is some code that will work with the JQuery Validation Plugin. I'm not sure if this will work with what you're using because I've never heard of it.

$("#request-form").validate({
    rules: {
        checkbox: { 
        required: 'input[type="checkbox"]:checked',
        minlength: $('input[type="checkbox"]').length()
        }
    },
    messages: {
        checkbox: "Please check at least one.",
    }
});

HTH

这篇关于jQuery复选框验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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