显示“必需"只需一个复选框(动态显示) [英] Displaying "required" in just one checkbox (dynamic display)

查看:40
本文介绍了显示“必需"只需一个复选框(动态显示)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用三种类型的问题创建了一个数据库驱动的测试-输入类型=单选,文本和复选框.如果我在结束标记前插入 required ,则对广播和文本问题(而不是复选框)正常工作.

I've created a database-driven test with three types of questions - input type = radio, text and checkbox. If I insert required before the closing tag, it works fine for the radio and text questions, but not the checkboxes.

问题在于,每个复选框问题都有几个可能的答案-从三到六个不等.正确答案的选择数量也有所不同.

The problem is that each checkbox question has several possible answers - anywhere from three to six. And the number of those choices that are correct answers also varies.

但是,当我在复选框脚本中添加必填"时,用户必须选择每个答案"才能进入结果页面.代码如下所示:

But when I add "required" to the checkbox script, the user has to select EVERY answer in order to proceed to the results page. This is what the code looks like:

<label for="q'.$QID.'-'.$Value.'"><input type="checkbox" name="q'.$QID.'[]" id="q'.$QID.'-'.$Value.'" value="'.$Value.'" **required**> '.$QA.'</label>

我找到了 HTML5必填属性页面,该页面似乎可以解决此问题.如果我理解正确,则可以通过仅在一个元素中插入"required"来解决问题.因此,如果将其插入答案A中,则用户可能会选择答案C和D,一切都会正常进行.

I found the HTML5 Required Attribute page, which appears to address this problem. If I understand correctly, you can fix the problem by inserting "required" in just one element. So if you insert it in answer A, a user could presumably choose answers C and D, and everything would work just fine.

如果这是正确的,那么是否有办法仅在一个复选框中而不是所有复选框中显示必需"?

If that's correct, then is there a way to make "required" display in just one checkbox, instead of all of them?

推荐答案

使用条件必需,并将条件用于组的第一个复选框.我假设您的html代码是用.php文件编写的.

Use required using a condition for the first checkbox of a group. I assume your html code is written in .php files.

$required = ($Value == 0) ? "required" : ""; //condition to check if the value is first. since it is database driven it would be better to check the key instead of the value.;
   echo '<label for="q'.$QID.'-'.$Value.'"><input type="checkbox" name="q'.$QID.'[]" id="q'.$QID.'-'.$Value.'" value="'.$Value.'" '. $required .'> '.$QA.'</label>';

另一种解决方案是使用Jquery添加必需的属性.例如,如果性别是复选框组的名称,则:

Another solution is to add required attribute using Jquery. For eg if gender is the name of the group of checkboxes, then:

$('input[type=checkbox][name=gender]')[0].attr('required', 'required');;

这篇关于显示“必需"只需一个复选框(动态显示)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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