我已經寫了一個函數,我希望應該檢查一個文本字段是否爲空,如果是,應該將焦點放回去。當用戶離開文本字段(模糊)時,檢查完成。不幸的是,代碼無法正常工作。爲什麼這樣?我正在使用playframework。這個問題是在JavaScript代碼。JavaScript函數無法處理模糊事件
@(form:Form[User2])
<!DOCTYPE html>
<html lang="en" xmlns:font-variant="http://www.w3.org/1999/xhtml">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-COMPATIBLE" content="IE=edge">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>HTML Test</title>
<link rel="stylesheet" type="text/css" href="@routes.Assets.at("stylesheets/bootstrap.min.css")">
<!--link rel="stylesheet" href="stylesheets/bootstrap-theme.min.css"-->
<style type="text/css">
html, body{height:100%; margin:0;padding:0}
<!-- this centers the texts fields -->
.center-form {
width:100%;
margin:auto;
position:relative;
top:50%;
transform: translateY(-50%)
}
</style>
<script>
/*this function works fine and puts the text cursor on first text field*/
function setup(){
var textInput;
textInput = document.getElementById('first-name');
textInput.focus();
}
var firstName;
firstName = document.getElementById('first-name');
/*the problem is in this code. I check if the field is empty and if so, I call focus to put focus on that field*/
function validateFirstName(){
var name = firstName.value;
if (name.length <= 0) {
alert("error");
firstName.focus();
}
}
window.addEventListener('load',setup,false);
firstName.addEventListener('blur',validateFirstName,false);
</script>
</head>
<body>
<div class="container center-form" >
<!-- for medium and large screens,
First row of Bootstrap grid contains logo. Total 3 columns (12/4). Logo in middle column-->
<div class="row" >
<!--empty column-->
<div class="col-md-4 col-lg-4" ></div>
<!--logo column-->
<!--div class="col-md-4 col-lg-4" >
<div>
<img src="@routes.Assets.at("images/somelogo.png")" alt="Logo" height="64" width="303">
</div>
</div-->
<!--empty column-->
<div class="col-md-4 col-lg-4"></div>
</div>
<!-- for medium and large screens,
Second row of Bootstrap grid contains the form for username and password. Total 3 columns (12/4). -->
<div class="row" >
<!--empty column-->
<div class="col-md-4 col-lg-4"></div>
<!--form-->
<div class="col-md-4 col-lg-4">
<form onsubmit='return onSubmit(this)'>
<div class="form-group">
<label for="first-name">First Name</label>
<input type="text" class="form-control" id="first-name" value="@form("name").value" required>
</div>
<div class="form-group">
<label for="last-name">Last Name</label>
<input type="text" class="form-control" id="last-name" value="@form("name").value" required>
</div>
<div class="form-group">
<label for="email">Email</label>
<input type="email" class="form-control" id="email" value="@form("email").value" required>
</div>
<div class="form-group">
<label for="password">Password</label>
<input type="password" class="form-control" id="password" required>
</div>
<div class="form-group">
<label for="confirm-password">Confirm password</label>
<input type="password" class="form-control" id="confirm-password" required>
</div>
<div class="form-group">
<label for="street-name">Street Name</label>
<input type="text" class="form-control" id="street-name" required>
</div>
<div class="form-group">
<label for="country">Country</label>
<input type="text" class="form-control" id="country" required>
</div>
<button type="submit" class="btn btn-primary">Sign Up</button>
</form>
</div>
<!--empty column-->
<div class="col-md-4 col-lg-4"></div>
</div>
</div>
<!--script src="@routes.Assets.at("javascripts/jquery-1.9.0.min")"></script-->
<!--script src="@routes.Assets.at("javascripts/bootstrap.min.js")"></script-->
</body>
</html>
你應該把'firstName = document.get ...'和'firstName.addEve ...'放在load事件監聽器('setup '功能)! **任何與DOM有關的事務必須等待DOM加載**。 –
謝謝。有效。我注意到的一個特殊行爲是,當我從「名字」字段移出而沒有輸入任何內容時,我會不斷地收到警報消息,而不是隻收到一次。我必須選擇「阻止此頁面創建新的框」來停止新的警報。爲什麼? –
我認爲'alert'對話框會從輸入中獲得焦點,所以每次都有一個警報彈出模糊事件被觸發,另一個警報彈出......我不確定,但是您必須對此進行更多的研究! –