2015-09-14 215 views
0

如何選擇一個單選按鈕,其值是唯一通過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

回答

2

對於多個元素,您不能具有相同的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

相關問題