weixin_33690963 2010-04-16 05:53 采纳率: 0%
浏览 60

使用复选框作为单选按钮

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.

  • 写回答

3条回答 默认 最新

  • weixin_33749242 2010-04-16 06:00
    关注

    Solution based on 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);" />
    

    Working Demo

    Solution based on 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" />
    

    Working Demo

    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?