Rails 3:如何使用户选择表单中的至少一个复选框? [英] Rails 3: How to make the user choose at least one checkbox in a form?

查看:89
本文介绍了Rails 3:如何使用户选择表单中的至少一个复选框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表单,用户必须在提交表单之前至少选中一个复选框.是否有任何插件可以处理此问题,或者可以将jQuery应用于我的表单?不幸的是,我是一个jquery新手.

I have a form, where a user has to check at least one checkbox before submitting the form. Is there any plugin that can handle this or maybe jquery that can be applied to my form? Unfortunately I'm a total jquery rookie.

推荐答案

jQuery验证插件可以提供此功能.您可以在此处此处看到有关如何实现此示例的演示. .

The jQuery Validation Plugin can provide this functionality. You can see a demo of how to implement it for requiring a checkbox here.

基本上,一旦在页面上加载了验证插件的JavaScript文件,就需要为要验证的表单初始化验证器.执行此操作的JavaScript如下所示:

Basically, once you load the JavaScript file for the validation plugin on your page, you'll need to initialize the validator for the form you want to validate. The JavaScript to do this will look something like:

$('#form1').validate();

需要将复选框包装在<fieldset>标记中.然后可以通过将validate属性添加到复选框的input标签来激活字段集的验证.例如:

The checkboxes will need to be wrapped in a <fieldset> tag. The validation of the fieldset can then be activated by adding a validate attribute to the input tag for the checkbox. For example:

    <input type="checkbox" validate="required:true" />

然后,当单击表单的提交"按钮时,将触发验证.

The validation will then be triggered when the form's submit button(s) are clicked.

这篇关于Rails 3:如何使用户选择表单中的至少一个复选框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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