我无法弄清楚为什么我的网页不会重定向到设置页面。只要满足条件,页面就会刷新。我已经进入浏览器控制台并粘贴了我的重定向代码,它确实重定向。
完整的JavaScript功能:
function formSubmit(){
var formFN = document.getElementById("fName");
var formLN = document.getElementById("lName");
if( formFN.value.length == 0 || formFN.value == null){
window.alert("Please enter your first name.");
return false;
}
else if( formLN.value.length == 0 || formLN.value == null){
window.alert("Please enter your last name.");
return false;
}
else
{
document.location = "resultPage.html";
return false;
}
}
HTML部分:
<div id="form">
<form action="">
<h3>Thanks for visiting!</h3>
<label for="fName">First Name:</label>
<input type="text" id="fName" value="">
<br>
<label for="lName">Last Name:</label>
<input type="text" id="lName" value="">
<br>
<button onclick="formSubmit();">
Submit
</button>
<!-- <input type="submit" value="Submit" onclick="formSubmit();"> -->
</form>
</div>
答案 0 :(得分:2)
默认情况下,button
个元素的float dist = sqrt((vv.x * vv.x) + (vv.z * vv.z));
dist = max(0, dist - offset);
vv = float4(0.0f, dist * dist * -_Curvature , 0.0f, 0.0f);
属性为type
。根据您的问题,您可能需要这样做:
submit
如果你想要一个更通用的解决方案,你最好在表单上捕获和处理<button type="button" onclick="formSubmit();">
Submit
</button>
事件,因为在submit
中按回车之类的事情也会触发提交。< / p>
input
window.addEventListener("load", function() {
document.getElementById("form").getElementsByTagName("form")[0].addEventListener("submit",function(event) {
event.preventDefault(); // Stop the normal action for this event
var formFN = document.getElementById("fName");
var formLN = document.getElementById("lName");
if( formFN.value == null || formFN.value.length == 0 ){
alert("Please enter your first name.");
}
else if( formLN.value == null || formLN.value.length == 0 ){
alert("Please enter your last name.");
}
else {
document.location = "resultPage.html";
}
});
});
编辑:我发现你应该在检查长度之前检查空值。 (如果<div id="form">
<form>
<h3>Thanks for visiting!</h3>
<label for="fName">First Name:</label>
<input type="text" id="fName" value="">
<br>
<label for="lName">Last Name:</label>
<input type="text" id="lName" value="">
<br>
<button>Submit</button>
</form>
</div>
为.value
,则检查null
将引发错误。)相应调整后的代码。