【问题标题】:jQuery Select all Radio button groupjQuery 全选单选按钮组
【发布时间】:2015-10-12 14:20:35
【问题描述】:

我正在处理一个包含多个单选按钮组的长表单。

在底部有一个单选按钮“No All”。当它被选中时,我想让它选择所有的“N”单选按钮,但我无法让它工作。这是代码的简化版本:

jQuery:

$(document).ready(function(){
//initially hide the Remove All Questions
$("div.#removeAllquest").hide();

////////////  Show Remove All Questions if radio button selected 
$("input.#RemoveAll").click(function() {
  if ($(this).is(':checked'))
    $("input:radio [class*='radioR']").attr("checked",true); 
  else
$("input:radio [class*='radioR']").attr("checked",false); 
});

});

表格:

<table>
  <tr>
    <td>Y<input type="radio" name="row1" value="row1col1" class="q123col1"></td>
    <td>N<input type="radio" name="row1" class="radioR" value="row1col2"></td>
    <td>M<input type="radio" name="row1" value="row1col3" class="q123col3"></td>
  </tr>
  <tr>
    <td>Y<input type="radio" name="row2" value="row2col1" class="q123col1"></td>
    <td>N<input type="radio" name="row2" class="radioR" value="row2col2"></td>
    <td>M<input type="radio" name="row2" value="row2col3" class="q123col3"></td>
  </tr>
  <tr>
    <td>Y<input type="radio" name="row3" value="row3col1" class="q123col1"></td>
    <td>N<input type="radio" name="row3" class="radioR" value="row3col2"></td>
    <td>M<input type="radio" name="row3" value="row3col3" class="q123col3"></td>
  </tr>
  <tr> 
    <td colspan="2">No All </td>
    <td>
      <input name="RemoveAll" id="RemoveAll" type="radio" value="Y">
    </td>
  </tr>
</table>

我做错了什么?

【问题讨论】:

    标签: jquery radio-button


    【解决方案1】:

    这两个都应该工作 - 我试图保持你的风格的第一个。第二个我改变了风格。对于您的示例,一旦它们被选中,就没有办法取消选中它们。

    $("input.#RemoveAll").click(function() {
        if ($(this).is(':checked'))
            $("input:radio.radioR").attr("checked", "checked");
        else
            $("input:radio.radioR").removeAttr("checked");
    });
    
    $("#RemoveAll").click(function() {
        if ($(this).is(':checked'))
            $(".radioR").attr("checked", "checked");
        else
            $(".radioR").removeAttr("checked");
    });
    

    【讨论】:

    • 谢谢安迪!!!我使用了你的第一个选项,它就像一个冠军。我想取消选中单选按钮...但我花了很长时间才走到这一步,我想现在这有点过头了:)
    • 我不认为你真的必须删除“checked”属性,因为你一次只能选择一个单选按钮,但你的其余代码看起来不错。
    【解决方案2】:

    首先,据我所知,checked 只接受 check 作为 XHTML 中的有效值。所以像下面这样的东西应该可以解决问题

    $("#RemoveAll").change(function() {
        if ($(this).is(':checked'))
                $("input:radio.radioR").attr("checked","checked"); 
    
        else
                $("input:radio.radioR").removeAttr("checked"); 
        });
    });
    

    请注意删除所有单选按钮的选择器更改,因为添加输入元素过滤器并不是真正必要的,因为 $("#id") 调用 document.getElementById。

    【讨论】:

      【解决方案3】:

      这应该可以满足您的需求...

      $("input.#RemoveAll").click(function() {
      
        if ($(this).attr('checked') === "checked"){
      
          $("input:radio[class*='radioR']").attr("checked","checked"); 
      
        }else{
      
          $("input:radio[class*='radioR']").removeAttr("checked"); 
      
        }
      });
      

      希望对你有帮助,思南。

      【讨论】:

        猜你喜欢
        • 2010-11-13
        • 2010-12-11
        • 2012-02-07
        • 1970-01-01
        • 1970-01-01
        • 2011-06-30
        • 1970-01-01
        • 1970-01-01
        • 2011-08-31
        相关资源
        最近更新 更多