简体   繁体   English

基于值的单选按钮选择

[英]Radio Button-selection based on value

how can i select a radio button with its value is the only one which is unique via JS. 我如何选择一个单选按钮,其值是通过JS唯一的按钮。 This seems to be little odd, but i need to. 这似乎有点奇怪,但我需要。 the Html code is as follows HTML代码如下

<td class="Customer_Name"><table>
<td>
     <input name="Name" tabindex="4" id="Name" type="radio" value="01">John</td>    
     <input name="Name" tabindex="4" id="Name" type="radio" value="02">Sam</td> 
     <input name="Name" tabindex="4" id="Name" type="radio" value="03">Fred</td>
     <input name="Name" tabindex="4" id="Name"  type="radio" value="04">Peter</td>              
<td>

You cannot have same id for multiple elements. 您不能为多个元素使用相同的id id should be unique. id应该是唯一的。

You can use querySelector with attribute-value selector. 您可以将querySelector与属性值选择器一起使用。

document.querySelector('input[type="radio"][value="02"]')

Demo 演示版

 document.querySelector('input[type="radio"][value="02"]').checked = true; 
 <td class="Customer_Name"> <table> <td> <input name="Name" type="radio" value="01" />John</td> <td> <input name="Name" type="radio" value="02" />Sam</td> <td> <input name="Name" type="radio" value="03" />Fred</td> <td> <input name="Name" type="radio" value="04" />Peter</td> <td> 


Using jQuery 使用jQuery

$('input[type="radio"][value="02"]').prop('checked', true);

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

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