我不知道为什么它不起作用,想要根据复选框值重定向到页面或什么都不做。这是代码
<html>
<body>
<form onsubmit= "lol()" >
Checkbox: <input type="checkbox" id="myCheck">
<input type="submit" value="Submit">
</form>
<script>
function lol()
{
if(document.getElementById("myCheck").checked == true)
{
window.location="http://www.google.com";
}
else
{
// want do nothing and stay at same page .
}
}
</script>
</body>
</html>
我该怎么做
最佳答案
如果您想让表单在错误条件下不执行任何操作,则需要执行以下两件事。
调用函数时需要使用return。这样表单在获得返回 true 值之前不会提交。
在函数的 else 部分中,您需要提到 return=false。它将停止提交表单。
Javascript:
function lol()
{
if(document.getElementById("myCheck").checked == true)
{
window.location.href="http://www.google.com";
}
else
{
return false;
}
}
HTML
<form onsubmit="return lol()">
Checkbox: <input type="checkbox" id="myCheck"/>
<input type="submit" value="Submit" />
</form>
关于javascript - 根据复选框使用java脚本重定向到页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24163532/