使用按钮检查两个或多个无线电组

时间:2016-10-29 14:53:14

标签: javascript html

我希望有人可以帮助我,因为我已经寻找解决方案,并尝试了许多不同的事情 - 我被卡住了!

fiddle也可以演示

我的代码是

$("#button_1").click(function() {
  $("INPUT[name=type]").val(['1']);
});

$("#button_2").click(function() {
  $("INPUT[name=type]").val(['2']);
});

$("#button_3").click(function() {
  $("INPUT[name=type]").val(['3']);
});
<button id="button_1">check both blue radios</button>
<button id="button_2">check both red radios</button>
<button id="button_3">check both yellow radios</button>
<br>
<br>
<div id='type'>
  <label for="radio_1">blue dial</label>
  <input type='radio' id='radio_1' name='type' value='1' />
  <label for="radio_2">red dial</label>
  <input type='radio' id='radio_2' name='type' value='2' />
  <label for="radio_3">yellow dial</label>
  <input type='radio' id='radio_3' name='type' value='3' />
</div>

<br>
<br>
<div id='type'>
  <label for="radio_1">blue strap</label>
  <input type='radio' id='radio_1' name='type' value='1' />
  <label for="radio_2">red strap</label>
  <input type='radio' id='radio_2' name='type' value='2' />
  <label for="radio_3">yellow strap</label>
  <input type='radio' id='radio_3' name='type' value='3' />
</div>

我正在做的是点击按钮并检查下面的相关TWO单选按钮。

我是新来的,所以任何帮助都会受到赞赏 - 谢谢!

2 个答案:

答案 0 :(得分:0)

问题是您的输入具有相同的名称属性。

顺便说一句,我改变了另外两件事:

  • ID属性 ID属性必须是唯一的!即使你不使用它。

  • 用户jQuery .prop()函数用于设置/取消设置checked个问题

    &#13;
    &#13;
    $("#button_1").click(function() {
    	$('input[value="1"]').prop('checked', true);
    });
    
    $("#button_2").click(function() {
      $('input[value="2"]').prop('checked', true);
    });
    
    $("#button_3").click(function() {
      $('input[value="3"]').prop('checked', true);
    });
    &#13;
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <button id="button_1">check both blue radios</button>
    <button id="button_2">check both red radios</button>
    <button id="button_3">check both yellow radios</button>
    <br>
    <br>
    <div id='type'>
      <label for="dial_1">blue dial</label>
      <input type='radio' id='dial_1' name='dial' value='1' />
      <label for="dial_2">red dial</label>
      <input type='radio' id='dial_2' name='dial' value='2' />
      <label for="dial_3">yellow dial</label>
      <input type='radio' id='dial_3' name='dial' value='3' />
    </div>
    
    <br>
    <br>
    <div id='type'>
      <label for="strap_1">blue strap</label>
      <input type='radio' id='strap_1' name='strap' value='1' />
      <label for="strap_2">red strap</label>
      <input type='radio' id='strap_2' name='strap' value='2' />
      <label for="strap_3">yellow strap</label>
      <input type='radio' id='strap_3' name='strap' value='3' />
    </div>
    &#13;
    &#13;
    &#13;

答案 1 :(得分:0)

试试这可能会解决您的问题: -

<form>
    <fieldset id="group1">
        <input type="radio" value="" name="group1">
        <input type="radio" value="" name="group1">
    </fieldset>

    <fieldset id="group2">
        <input type="radio" value="" name="group2">
        <input type="radio" value="" name="group2">
        <input type="radio" value="" name="group2">
    </fieldset>
</form>