繁体   English   中英

如何取消选中JS中的单选按钮?

[英]How to uncheck a radio button in JS?

我很难过这一点。 ID号始终会更改(这种情况下为_t2342),因此我无法正确选择它。 我正在尝试使用JavaScript从输入字段中删除“ checked =“ true”。可能吗?我只有2个项目的数组。

<table id="inputFormPopup:_t2342">
  <tbody>
    <tr>
      <td>
        <input checked="true" id="inputFormPopup:_t2342:0" name="inputFormPopup:_t2342" onchange="document.getElementById('inputFormPopup:submitInptExtendPricing').click();document.getElementById('inputFormPopup').reset();" type="radio" value="ef">
        <label
          for="inputFormPopup:_t2342:0"> Effective Date</label>
      </td>
    </tr>
    <tr>
      <td>
        <input id="inputFormPopup:_t2342:1" name="inputFormPopup:_t2342" onchange="document.getElementById('inputFormPopup:submitInptExtendPricing').click();document.getElementById('inputFormPopup').reset();" type="radio" value="ex"><label for="inputFormPopup:_t2342:1"> Expiration Date</label></td>
    </tr>
    <tr>
      <td>

    </tr>
  </tbody>
</table>

获取DOM元素并将checked属性设置为true ,如下所示:

var $button = document.getElementById('inputFormPopup:_t2342:1')
$button.setAttribute('checked',true);//this will check the radio
//$button.setAttribute('checked',false);//this will uncheck the radio

如果ID发生变化,则可以考虑使用jquery按钮。 例如: $('table > tr > td > input[type=radio]').setAttribute('checked', false);

如果页面中只有一个表,则可以从表开始选择目标元素:

document.querySelector('table input[type=radio][checked=true]').checked = false;

或通过使用选择器开头的属性

document.querySelector('table[id^=inputFormPopup] input[type=radio][checked=true]').checked = false;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM