問題是我無法獲取密碼字段的值 - 使用pso1的警報返回null。我在代碼中看不到任何錯誤,所以我要求(求助)求助。 JS沒有從輸入中獲得價值
<form class='container' onsubmit="return checkit()">
<h1> Javascript </h1>
Login <input type='text' id='log'><br>
Password <input type='password' id='pso1'><br>
Confirm passwordd <input type='password' id='pso2'><br>
<button>Register</button>
</form>
<script type="text/javascript">
var pso1=document.getElementById('pso1').value ;
var pso2=document.getElementById('pso2').value ;
alert(pso1);
function checkit(){
if(pso1=!pso2){
return false;
alert('Passwords are not the same.');
}
else{
return true; alert('work');
}
}
</script>
</body>
當頁面加載時,您正在設置函數外部的變量值。 – TZHX
在'return'之後放置'alert()'不起作用。 'return'離開函數,在執行後沒有任何東西。 – Barmar