,所以我有一個HTML頁面調用的index.html,它看起來像:爲什麼我的視圖更新時,我有我的功能警報但是當我做不不
<!DOCTYPE html>
<html>
<head>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<title>sometitle</title>
<link rel="stylesheet" href="style.css" />
</head>
<body>
<div id="Screen">
<section class="loginform cf">
<form action="#" method="post" id="loginwindow">
<ul>
<li>
<label for="usermail">Email</label>
<input type="email" name="useremail" placeholder="[email protected]" required>
</li>
<li>
<label for"password">Password</label>
<input type="password" name="password" placeholder="password" required>
</li>
<li>
<input type="submit" value="Check In">
</li>
</ul>
</form>
</section>
<script src="js/index.js"></script>
</div>
</body>
</html>
,它有一個js文件名爲index.js看起來像這樣:
window.onload = function() {
var loginForm = document.getElementById('loginwindow');
if (loginwindow) {
loginwindow.onsubmit = function() {
var useremail = document.getElementById('useremail');
var password = document.getElementById('password');
// Make sure javascript found the nodes:
if (!useremail || !password) {
load("LoopIt_DashBoard.html");
return "success";
}
}
}
}
function load(url)
{
$("#Screen").load(url, function(response, status, xhr) {
if (status == "error") {
var msg = "Sorry but there was an error: ";
$("#error").html(msg + xhr.status + " " + xhr.statusText);
}
});
test();
}
function test()
{
alert("test");
}
function test(string)
{
alert(string);
}
,並呼籲LoopIt_DashBoard.html第二HTML文件看起來像這樣:
<div id="Screen">
<section class="loginform cf">
<form action="#" method="post" id="loginwindow">
<ul>
<li>
<input type="submit" value="Check In">
</li>
</ul>
</form>
</section>
</div>
現在這個代碼所做的就是獲得一封電子郵件和一個密碼,然後比較兩者。如果它們相等,那麼它將使用LoopIt_DashBoard.html文件中的代碼以id Screen更改div中的html。但是,如果我拿出這只是調用警報(我用它測試)
test();
從
function load(url)
{
$("#Screen").load(url, function(response, status, xhr) {
if (status == "error") {
var msg = "Sorry but there was an error: ";
$("#error").html(msg + xhr.status + " " + xhr.statusText);
}
});
test();
}
那麼這個功能不起作用線,頁面不會改變。如何讓我的視圖更新而不使用警報。
嘗試使用返回true或返回false(根據您的要求),而不是測試() –
也許無關緊要,但是在您的onload中,您設置了loginForm,然後測試了loginwindow。另外,「測試」的兩個定義並不按我想要的方式工作。第二個重新定義第一個(寫在它上面)。 – DrC