我试图使用复选框取消选中来清除单选按钮。我能够得到它的样品。但是在我的表格中,您不止一次有相同的设置。我想要一个可以跨集合使用的通用方法。
可以帮帮我。这是我的代码。我想为此使用java脚本。
<html>
<head>
<script language="JavaScript">
function loopForm(form) {
var cbResults = 'Checkboxes: ';
var radioResults = 'Radio buttons: ';
for (var i = 0; i < form.elements.length; i++ ) {
if (form.elements[i].type == 'checkbox') {
if (form.elements[i].checked == true) {
cbResults += form.elements[i].value + ' ';
}
}
if (form.elements[i].type == 'radio') {
if (form.elements[i].checked == true) {
radioResults += form.elements[i].value + ' ';
}
}
}
document.getElementById("cbResults").innerHTML = cbResults;
document.getElementById("radioResults").innerHTML = radioResults;
}
function loopForm123(form) {
for (var i = 0; i < form.elements.length; i++ ) {
if (form.elements[i].type == 'checkbox') {
if (form.elements[i].checked == true) {
alert('insde for loop1');
form.elements[i].checked == false;
alert('insde for loop3');
}
}
if (form.elements[i].type == 'radio') {
if (form.elements[i].checked == true) {
alert('insde for loop2');
form.elements[i].checked == false;
alert('insde for loop4');
}
}
}
}
</script>
<body>
<form name="thisForm">
I like to program in:<p>
<input type="checkbox" value="PHP" >PHP<p>
<input type="checkbox" value="Perl">Perl<p>
<input type="checkbox" value="Ruby">Ruby<p>
<input type="checkbox" value="ASP">ASP<p>
<hr>
I like to eat:<p>
<input type="radio" value="Snickers" name="candy" >Snickers<p>
<input type="radio" value="Hershey's" name="candy">Hershey's<p>
<input type="radio" value="M&M's" name="candy">M&M's<p>
<input type="radio" value="Nerds" name="candy">Nerds<p>
<hr>
I like to drink:<p>
<input type="radio" value="Coke" name="drink" >Coke<p>
<input type="radio" value="Gatorade" name="drink">Gatorade<p>
<input type="radio" value="Pepsi" name="drink">Pepsi<p>
<input type="radio" value="Milk" name="drink">Milk<p>
<br>
<input type="checkbox" value="Submit" onclick="loopForm123(document.thisForm);">
</form>
<p>
<div id="cbResults"></div>
<div id="radioResults"></div>
</body>
</html>
JSFiddle:http: //jsfiddle.net/zzrVN/