[英]Text input validation function Javascript
有人可以告訴我為什么我的代碼不起作用嗎? 我收到錯誤:無法讀取null的屬性“值”。
這是我的html代碼:
<body onLoad="init()">
<input type="text" placeholder="User Name" id="id_inputuser"></input>
<label>Please Enter User Name</label><br>
<input type="text" placeholder="Password"id="id_inputpass"></input>
<label>Please Enter Password</label><br><br>
<button onClick="verify()">Enter</button>
</body>
這就是JS:
function init()
{
var user= document.getElementById("id_inputuser").value;
function verify()
{if (user=="david")
{alert("working")}
else{alert("not working")};
}}
任何幫助將不勝感激:)非常感謝!
如果您說這是完整的代碼,那么這是可行的
<!DOCTYPE html>
<html>
<body>
<input type="text" placeholder="User Name" id="id_inputuser"></input>
<label>Please Enter User Name</label><br>
<input type="text" placeholder="Password"id="id_inputpass"></input>
<label>Please Enter Password</label><br><br>
<button onClick="verify()">Enter</button>
<script>
function verify()
{
var user= document.getElementById("id_inputuser").value;
if (user=="david")
{alert("working")}
else{alert("not working")};
}
</script>
</body>
</html>
var input...
行移到verify()
函數內。 例如
<input type="text" id="username" />
<script>
function checkUsername () {
var value = document.getElementByid('username').value;
alert( value ? 'Valid' : 'Empty' );
}
document.getElementById('username').addEventListener('change', checkUsername);
</script>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.