我正在尝试使用js
登录,我知道这是不安全的,它只是为了学校-他们要求它。
如果用户名和密码正确,我希望它打开我的照片页面,这是不会发生的。
据我所知,其他一切都运行得很好,它可以正确地收集和比较数据,但不会打开新页面。我在this.location.href
上尝试过window.location
和location.replace
等变体。
我还尝试过使用full URL
和photos.php更改URL
该函数是从HTML
中的单击调用的:
<button type="submit" onclick="checkPassword()" class="button">Login</button>
这是JavaScript:
function check() { //onclick
var username = "test";
var password = "admin";
var usernameEntered = document.getElementById("username").value;
var passwordEntered = document.getElementById("password").value;
if (usernameEntered == username && passwordEntered == password) {
this.location.href = "http://localhost/photos-v4/photos.php";
} else {
alert("Incorrect username or password");
}
}
发布于 2018-08-20 10:17:43
问题出在HTML中。
type = "submit"
用于将数据发送到新位置,这意味着数据不会被JavaScript捕获。
一个解决方案是用reset
替换submit
,它会清除表单,并让js获得所需的数据。
你可以在这里阅读更多关于超文本标记语言按钮的内容:https://html.com/attributes/button-type/
发布于 2018-06-19 15:50:02
这里有几种方法
在新页面中打开
window.open('http://localhost/photos-v4/photos.php', '_blank').location;
在当前标记中打开
window.location.href = 'http://localhost/photos-v4/photos.php'
https://stackoverflow.com/questions/50922792
复制相似问题