我正在尝试使用Javascript验证单选按钮。但我无法得到它。任何人都可以帮我解决这个问题。我附上了我的代码。感谢。
<form action="submitAd.php" method="POST" enctype="multipart/form-data" name="packages" onsubmit="return checkForm()">
<div id="plans_pay">
<input type="radio" name="group1" id="r1" value="Office" onchange="click_Pay_Office()" style="float:left;margin-top:20px;font-size:72px;">
<label style="float:left; margin-top:20px;" for="pay_office">At Our Office</label>
<img style="float:left;margin-bottom:10px;" src="images/Pay-at-office.png" />
</div>
<div id="plans_pay">
<input style="float:left;margin-top:20px;font-size:72px;" type="radio" name="group1" id="r2" value="HNB" onchange="click_Pay_Hnb()">
<label style="float:left; margin-top:20px;" for="pay_hnb">At Any HNB Branch</label>
<img style="float:left;margin-bottom:10px;" src="images/HNB.png" />
</div>
</form>
的Javascript
function checkForm(){
if( document.packages.pso.checked == false && document.packages.pso1.checked == false && document.packages.ph.checked == false && document.packages.ph2.checked == false && document.packages.ph3.checked == false && document.packages.pl.checked == false && document.packages.p3.checked == false && document.packages.p4.checked == false && document.packages.p5.checked == false && document.packages.p6.checked == false ){
alert('Please Select At Least One Package');
return false;
}
if( document.packages.pso.checked == false && document.packages.pso1.checked == false && document.packages.ph.checked == false && document.packages.ph.checked == false && document.packages.ph2.checked == false && document.packages.ph3.checked == false && document.packages.pl.checked == false && document.packages.p3.checked == false && document.packages.p4.checked == false && document.packages.p5.checked == false && document.packages.p6.checked == false){
alert('Please Select At Least One with the Advertise online option in premium package');
return false;
}
if(document.getElementById('words').value==''){
alert("Please Enter the Texts");
return false;
}
if(document.getElementById('r1').checked==false && document.getElementById('r2').checked==false){
alert("Please Select a Payment Method");
return false;
}
}
答案 0 :(得分:0)
不要在事件 onsubmit 上的表单标记中调用 checkForm(),而是在提交按钮中更好地使用它
<input type="submit" name="submit" value="submit" onClick="return checkForm()" />
它会更好地工作并根据需要输出。