【发布时间】:2017-11-02 12:59:40
【问题描述】:
我正在构建多个复选框。我想添加功能,在一组复选框中,只有一个复选框应该检查正确或错误的值。选择所有组后,提交按钮应启用。单击提交按钮后,应显示每个组的值(正确或错误答案)。我该怎么做?
function isChecked(checkbox, sub1) {
document.getElementById(sub1).disabled = !checkbox.checked;
}
$("input:checkbox").on('click', function() {
var $box = $(this);
if ($box.is(":checked")) {
var group = "input:checkbox[name='" + $box.attr("name") + "']";
$(group).prop("checked", false);
$box.prop("checked", true);
//alert("checked");
} else {
$box.prop("checked", false);
}
var bool;
$("input.checkbox").change(function() {
bool = $(".checkbox:not(:checked)").length != 6;
// enable/disable
$("#submitbutton").prop('disabled', bool).addClass('btn');
// $("#submitbutton").removeAttr("disabled", bool).addClass("btn");
//$('#submitbutton').removeClass('btn1').prop(':disabled', bool).addClass('btn');
<!-- alert('right')-->
}).change('color');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<input type="checkbox" class="checkbox checkbox_1" id="button0" name="fooby[0][]" value="chk0" />
<input type="checkbox" class="checkbox checkbox_1" id="button1" name="fooby[0][]" value="chk0" />
<br>
<input type="checkbox" class="checkbox checkbox_1" id="button2" name="fooby[1][]" value="chk1" />
<input type="checkbox" class="checkbox checkbox_1" id="button3" name="fooby[1][]" value="chk2" /><br>
<input type="checkbox" class="checkbox checkbox_1" id="button4" name="fooby[2][]" value="chk3" />
<input type="checkbox" class="checkbox checkbox_1" id="button5" name="fooby[2][]" value="chk4" />
<br>
<input type="checkbox" class="checkbox checkbox_1" id="button6" name="fooby[3][]" value="chk5" />
<input type="checkbox" class="checkbox checkbox_1" id="button7" name="fooby[3][]" value="chk6" />
<br>
<input type="checkbox" class="checkbox checkbox_1" id="button8" name="fooby[4][]" value="chk7" />
<input type="checkbox" class="checkbox checkbox_1" id="button9" name="fooby[4][]" value="chk8" />
<br>
<input type="checkbox" class="checkbox checkbox_1" id="button10" name="fooby[5][]" value="chk9" />
<input type="checkbox" class="checkbox checkbox_1" id="button11" name="fooby[5][]" value="chk10" /> <br>
<input type="submit" value="Submit" id="submitbutton" disabled="disabled" class="btn" />
【问题讨论】:
-
“在一组复选框中,只有一个复选框应该检查正确或错误的值”——为什么不使用单选按钮?这样就不需要 JavaScript。
-
感谢您分享您的问题 Blazemonger。在我的整个项目中,我只使用复选框。所以我更喜欢保持原样。
-
@ArunJack 从用户体验的角度来看,这是一种非常糟糕的网站设计方式。
-
这不是一个好的理由。如果我理解正确,您所描述的功能正是单选按钮的功能。还有 UX(用户体验)的问题——用户会看到复选框(正方形)而不是单选按钮(圆圈)并期望某种行为。
-
这个想法有点疯狂,但可以使用切换按钮来做到这一点(http://bootstrapswitch.com/examples.html 或https://www.w3schools.com/howto/tryit.asp?filename=tryhow_css_switch 如何)
标签: javascript jquery css arrays html