基于值的单选按钮选择

问题描述:

如何选择一个单选按钮,其值是唯一通过JS唯一的按钮。这似乎有点奇怪,但我需要。 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> 
+1

为什么所有的元素都具有相同的ID。 ID应该是唯一的一个良好的HTML结构 – bugwheels94

对于多个元素,您不能具有相同的idid应该是唯一的。

您可以使用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>


使用jQuery

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

thanks.how我可以得到它标记检查。 – justin

+0

@justin检查演示,在单选按钮 – Tushar

+0

上使用'checked'属性谢谢,我刚刚看到一个这样的结构的网页,是新的HTML和JS。你的代码工作,并感谢您的知识。 – justin