我在 HTML 和 JavaScript 验证方面遇到了一些问题。所以基本上我有多个字段,并且我将在移动到另一个 HTML 页面之前执行验证。这是代码:
<fieldset>
Name* <input id="name" type="text" name="name"> </input>
NRIC/Passport No* <input id="nric" type="text" name="nric" > </input>
<input id="btnSubmit" type="submit" value="Pay now!" onclick="validate()"/>
</fieldset>
我的 JavaScript:
function validate(){
var name = document.getElementById('name').value;
var nric = document.getElementById('nric').value;
var submit = document.getElementById('btnSubmit').value;
if(name == null || name == ""){
alert('Please enter your name. Do not leave it blank before proceed to the next field.');
}
else if(nric == null || nric == ""){
alert('Please enter your NRIC. Do not leave it blank before proceed to the next field.');
}else{
window.location.href = '../successful.html';
}
}
我尝试在移动到 success.html 之前执行验证。验证有效,但是,当我尝试导航到另一个页面时,它告诉我找不到该网页。有什么想法可以解决这个问题吗?
提前致谢。
最佳答案
window.location.href = '../successful.html';
上面这行可以被引用如下:
- 我想导航到另一个文件
window.location.href
- 此文件位于我当前的文件夹之外
- 准确地说,向后一个文件夹
../
- 我要访问的文件名为
successful.html
您的重定向将假定您具有以下项目组织:
\...
\MYPRECEDENTFOLDER
successful.html
\MYCURRENTFOLDER
myCurrentfile.html
您必须检查 success.html 的位置。另一种解决方案是调用绝对 URL(注意 URL 的更改,基本上是当您从本地主机转到另一个 URL 时)。
关于单击 JavaScript 按钮导航到另一个 HTML 页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31810610/