[英]How to clear radio button in Javascript?
I have a radio button named "Choose" with the options yes and no.我有一个名为“选择”的单选按钮,其中包含选项是和否。 If I select any one of the options and click the button labeled "clear", I need to clear the selected option, using javascript.
如果我选择任何一个选项并单击标记为“清除”的按钮,我需要使用 javascript 清除所选选项。 How can I accomplish that?
我怎样才能做到这一点?
You don't need to have unique id
for the elements, you can access them by their name
attribute:您不需要元素的唯一
id
,您可以通过它们的name
属性访问它们:
If you're using name="Choose"
, then:如果您使用的是
name="Choose"
,则:
With jQuery it is as simple as:使用 jQuery,它很简单:
\n$('input[name=Choose]').attr('checked',false);$('input[name=Choose]').attr('checked',false);\n
or in pure JavaScript:或在纯 JavaScript 中:
\n var ele = document.getElementsByName("Choose");var ele = document.getElementsByName("选择");\n for(var i=0;i<ele.length;i++)
for(var i=0;i<ele.length;i++)\n ele[i].checked = false;
ele[i].checked = false;\n
This should work.这应该有效。 Make sure each button has a unique ID.
确保每个按钮都有唯一的 ID。 (Replace Choose_Yes and Choose_No with the IDs of your two radio buttons)
(用两个单选按钮的 ID 替换 Choose_Yes 和 Choose_No)
document.getElementById("Choose_Yes").checked = false;
document.getElementById("Choose_No").checked = false;
An example of how the radio buttons should be named:应如何命名单选按钮的示例:
<input type="radio" name="Choose" id="Choose_Yes" value="1" /> Yes
<input type="radio" name="Choose" id="Choose_No" value="2" /> No
If you do not intend to use jQuery, you can use simple javascript like this如果你不打算使用 jQuery,你可以像这样使用简单的 javascript
document.querySelector('input[name="Choose"]:checked').checked = false;
Only benefit with this is you don't have to use loops for two radio buttons这样做的唯一好处是您不必为两个单选按钮使用循环
不是更好的选择是添加第三个按钮(“都不是”),它会给出与没有选择相同的结果吗?
一种清除一组单选按钮的 ES6 方法:
Array.from( document.querySelectorAll('input[name="group-name"]:checked'), input => input.checked = false );
In my case this got the job done:在我的情况下,这完成了工作:
const chbx = document.getElementsByName("input_name");
for(let i=0; i < chbx.length; i++) {
chbx[i].checked = false;
}
Simple, no jQuery required:简单,不需要 jQuery:
<a href="javascript:clearChecks('group1')">clear</a>
<script type="text/javascript">
function clearChecks(radioName) {
var radio = document.form1[radioName]
for(x=0;x<radio.length;x++) {
document.form1[radioName][x].checked = false
}
}
</script>
if the id of the radio buttons are 'male' and 'female', value reset can be done by using jquery如果单选按钮的 id 是 'male' 和 'female',则可以使用 jquery 进行值重置
$('input[id=male]').attr('checked',false);
$('input[id=female]').attr('checked',false);
If you have a radio button with same id, then you can clear the selection如果您有一个具有相同 id 的单选按钮,那么您可以清除选择
Radio Button definition :单选按钮定义:
<input type="radio" name="paymentType" id="paymentType" value="CASH" /> CASH
<input type="radio" name="paymentType" id="paymentType" value="CHEQUE" /> CHEQUE
<input type="radio" name="paymentType" id="paymentType" value="DD" /> DD
Clearing all values of radio button:清除单选按钮的所有值:
document.formName.paymentType[0].checked = false;
document.formName.paymentType[1].checked = false;
document.formName.paymentType[2].checked = false;
...
YES<input type="radio" name="group1" id="sal" value="YES" >
NO<input type="radio" name="group1" id="sal1" value="NO" >
<input type="button" onclick="document.getElementById('sal').checked=false;document.getElementById('sal1').checked=false">
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.