[英]using checkbox as radio button
I am looking for a way for users to select one of the two options (strength or weakness) for a list of qualities. 我正在寻找一种方法让用户选择两个选项之一(强度或弱点)作为质量列表。
for eg: 例如:
strength weakness not applicable
1. Communication
2. Punctuality
...
Radio button lets me select either a strength or weakness. 单选按钮让我选择强度或弱点。 However, I want the user to check only those qualities that apply and if a user accidentally selects a quality there is no way to undo the selection for a radio button unless there is a third radio button called not applicable or have the user re-enter the page.
但是,我希望用户只检查那些适用的质量,如果用户意外选择了质量,则无法撤消单选按钮的选择,除非有第三个不适用的单选按钮或让用户重新输入这一页。 I was wondering if there is a way to be able to get the flexibility of a checkbox (check / uncheck) in addition to disabling or enabling the other checkbox when one of them is checked or unchecked instead of using three radio buttons.
我想知道是否有办法能够获得复选框(检查/取消选中)的灵活性,除了在选中或取消选中其中一个而不是使用三个单选按钮时禁用或启用另一个复选框。
I don't think I have seen this behavior before so wondering if there is a more elegant way of doing this. 我不认为我之前已经看过这种行为,所以想知道是否有更优雅的方式来做到这一点。 I am open to other ideas to get the same functionality.
我愿意接受其他想法以获得相同的功能。 Using a checkbox as radio button was just a thought.
使用复选框作为单选按钮只是一个想法。
thanks much. 非常感谢。
Solution based on javascript 基于javascript的解决方案
function SetSel(elem)
{
var elems = document.getElementsByTagName("input");
var currentState = elem.checked;
var elemsLength = elems.length;
for(i=0; i<elemsLength; i++)
{
if(elems[i].type === "checkbox")
{
elems[i].checked = false;
}
}
elem.checked = currentState;
}
<input type="checkbox" class="chkclass" onclick="SetSel(this);" />
<input type="checkbox" class="chkclass" onclick="SetSel(this);" />
<input type="checkbox" class="chkclass" onclick="SetSel(this);" />
<input type="checkbox" class="chkclass" onclick="SetSel(this);" />
Solution based on jQuery 基于jQuery的解决方案
$(function(){
$("input:checkbox.chkclass").click(function(){
$("input:checkbox.chkclass").not($(this)).removeAttr("checked");
$(this).attr("checked", $(this).attr("checked"));
});
});
<input type="checkbox" class="chkclass" />
<input type="checkbox" class="chkclass" />
<input type="checkbox" class="chkclass" />
<input type="checkbox" class="chkclass" />
You should not use checkboxes as radio buttons (or vice-versa): this is inconsistent with the user's mental model, so it confuses people. 您不应该使用复选框作为单选按钮(反之亦然):这与用户的心理模型不一致,因此会让人感到困惑。
This is a problem with no ideal solution, but your initial suggestion of having a "not applicable" option as part of a group of 3 radio buttons is fairly common. 这是一个没有理想解决方案的问题,但是您最初建议将“不适用”选项作为一组3个单选按钮的一部分是相当普遍的。 If you pre-select the "not applicable" option by default and perhaps de-emphasize it visually (eg. gray it out) then from the user's point of view it will be almost as if there are only 2 options, but they can recover if they accidentally select one and want to "unselect" it.
如果您默认情况下预先选择“不适用”选项,并且可能在视觉上不加强调(例如,将其灰显),那么从用户的角度来看,它几乎就像只有2个选项,但它们可以恢复如果他们不小心选择了一个并想要“取消选择”它。
this is correct form, "checked" is a proprietary and not attribute 这是正确的形式,“已检查”是专有而非属性
$(function(){
$("input:checkbox.chkclass").each(function(){
$(this).change(function(){
$("input:checkbox.chkclass").not($(this)).prop("checked",false);
$(this).prop("checked", $(this).prop("checked"));
});
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.