显示“需要”在一个复选框(动态显示) [英] Displaying "required" in just one checkbox (dynamic display)
问题描述
我创建了一个数据库驱动测试,有三种类型的问题 - 输入类型= radio,text和checkbox。如果我在结束标记之前插入必需,它对广播和文字问题很有效,但不是复选框。
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.
但是当我添加required到复选框脚本时,用户必须选择每一个答案为了继续到结果页面。这是代码的样子:
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.
如果这是正确的,
推荐答案
使用必填
使用组的第一个复选框的条件。我假设你的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添加必需的属性。例如,如果gender是复选框组的名称,则:
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屋!