[英]Radio Button-selection based on value
我如何選擇一個單選按鈕,其值是通過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>
您不能為多個元素使用相同的id
。 id
應該是唯一的。
您可以將querySelector
與屬性值選擇器一起使用。
document.querySelector('input[type="radio"][value="02"]')
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);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.