【发布时间】:2018-02-26 11:07:14
【问题描述】:
我看不出为什么我的代码在处理我的单选按钮时没有将我带到指定的网页:
<form name="login" onsubmit="return validateForm()" method="post">
.
.
<input type="radio" name="ans1" id="yes">Yes<br>
<input type="radio" name="ans2" id="no">No<br>
<input type="submit" value="Login" onclick= "where_to_go();">
</form>
where_to_go() 的脚本:
function where_to_go(){
if (document.login.getElementById('yes').checked) {
// Yes is chosen
document.location.href = "Admin_index.html";
} else if (document.login.getElementById('no').checked) {
// No is chosen
document.location.href ="Log-In_Greet.php";
}
}
谁能告诉我哪里出错了?
【问题讨论】:
-
首先,您希望为单选按钮使用相同的
name属性,这样用户就无法选择多个单选按钮。其次,对于脚本,尝试使用document.getElementById('yes').checked,当你有一个id时,不需要更多的选择器,因为应该只有一个元素带有id。 -
你能把它设置成一个 sn-p 并向我们展示你的 validateForm() 函数
标签: javascript forms button radio