我想uncheck一个radiobutton我发现了一个方式做这个,但同时单选按钮的表单中的代码将无法正常工作,这是我的脚本为unchecking选中的单选按钮
<script type="text/javascript">
var allRadios = document.getElementsByName(1);
var booRadio;
var x = 0;
for(x = 0; x < allRadios.length; x++){
allRadios[x].onclick = function() {
if(booRadio == this){
this.checked = false;
booRadio = null;
}else{
booRadio = this;
}
}
}
</script>
<form name="theForm" target="_blank" action="laravel.php">
<label for="test">test</label>
<input type="radio" id="1" class="radio" name="1" value="1">
<input type="radio" id="2" class="radio" name="1" value="2">
<input type="radio" id="3" class="radio" name="1" value="3">
<input type="radio" id="4" class="radio" name="1" value="4">
</form>
Run Code Online (Sandbox Code Playgroud)
当单选按钮在表单标签之外时,脚本工作正常,但当无线电输入在表单中时,它无法工作我想知道我在这里缺少什么