首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用jQuery验证验证多个复选框

使用jQuery验证验证多个复选框
EN

Stack Overflow用户
提问于 2013-02-20 04:11:14
回答 2查看 18.9K关注 0票数 10

我正在尝试使用多个复选框,并确保使用jQuery验证至少选中其中一个复选框。到目前为止,我还没有任何运气。我遗漏了什么?我知道我的验证在那里,因为它适用于其他字段,只是不适用于我的复选框。我把代码放到jfiddle上了,也许这会有帮助。

编辑:我为我的输入name=list参数(list[])添加了括号。另外,在我的规则中,我将name参数从list更改为'list[]‘。我的旧代码如下所示。谢谢你,Sparky!

代码语言:javascript
运行
复制
OLD: <input type='checkbox' name='list' id='fullProduct'></input>

FIXED: <input type='checkbox' name='list[]' id='fullProduct'></input>

这是我的代码。

代码语言:javascript
运行
复制
$("#tradeForm").validate({
        rules: {
            startDate: {
                required: true,
                date: true
            },
            endDate: {
                required: true,
                date: true
            },
            showName: {
                required: true,
                minlength: 5
            },
            location: {
                required: true
            },
            list: {
                required: true
            }
        },
        messages: {
            startDate: "*",
            endDate: "*"
    }
});

                <table>
                <tr>
                    <th>Name of Show</th>
                    <td> <input type='text' name='showName'></input></td>
                </tr>
                <tr>
                    <th>Location</th>
                    <td><input type='text' name='location'></input></td>
                </tr>
                <tr>
                    <th><span style='padding-right: 50px;'>Select Literature</span></th>
                    <td><input type='checkbox' name='list' id='fullProduct'></input><span style='font-size: 12px;'>Guide One</span></td>
                    <td><input type='checkbox' name='list' id='oilProduct'></input><span style='font-size: 12px;'>Guide Two</span></td>
                </tr>                               
                <tr>                                
                    <td></td>                       
                    <td><input type='checkbox' name='list' id='diamondProduct'></input><span style='font-size: 12px;'>Guide Three</span></td>
                    <td><input type='checkbox' name='list' id='motorProduct'></input><span style='font-size: 12px;'>Guide Four</span></td>
                </tr>                               
            </table>
EN

Stack Overflow用户

发布于 2013-02-20 04:14:43

如果您只想始终选中其中一个复选框,请改用input type="radio"

如果没有,请尝试将复选框的名称属性更改为list[]。因为可以有多个校验值,所以它必须包含括号以指示它是一个数组。否则,该值将被覆盖。

票数 2
EN
查看全部 2 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14966465

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档