jQuery jQuery选择所有单选按钮组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1337759/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 11:15:06  来源:igfitidea点击:

jQuery Select all Radio button group

jqueryradio-button

提问by Chnikki

I'm working on a long form that has several radio button groups.

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

At the bottom there is a radio button "No All". When it's selected I would like to make it so that all of the "N" radio buttons are selected, but I can't get it to work. Here is a simplified version of the code:

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

jQuery:

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); 
});

});

});

Form:

形式:

<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>

What am I doing wrong?

我究竟做错了什么?

回答by Andy Gaskell

Both of these should work - with the first one I tried to keep your style. With the second I changed the style a bit. With your sample, there's not really a way to uncheck them once they've been checked.

这两个都应该有效 - 第一个我试图保持你的风格。第二个我稍微改变了风格。对于您的样本,一旦它们被检查,就没有真正的方法可以取消选中它们。

$("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");
});

回答by John Foster

Firstly as far as I'm aware checked only accepts checked as a valid value in XHTML. So something like the following should do the trick

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

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

    else
            $("input:radio.radioR").removeAttr("checked"); 
    });
});

Note the change of selector for the remove all radio button as adding an input element filter isn't really necessary as $("#id") calls document.getElementById.

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

回答by Sinan

This should do what you need...

这应该做你需要的......

$("input.#RemoveAll").click(function() {

  if ($(this).attr('checked') === "checked"){

    $("input:radio[class*='radioR']").attr("checked","checked"); 

  }else{

    $("input:radio[class*='radioR']").removeAttr("checked"); 

  }
});

hope it helps, Sinan.

希望能帮到你,思南。