2016-09-16 40 views
0

問題是我無法獲取密碼字段的值 - 使用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> 
+3

當頁面加載時,您正在設置函數外部的變量值。 – TZHX

+0

在'return'之後放置'alert()'不起作用。 'return'離開函數,在執行後沒有任何東西。 – Barmar

回答

1

在你的代碼是在頁面加載後,一旦其權值改變之前設置的pso1ps02值。你會想,而不是更新這些值,聲明他們作爲自己的函數中的局部變量:

function checkit(){ 
    var pso1=document.getElementById('pso1').value; 
    var pso2=document.getElementById('pso2').value; 
    if(pso1=!pso2){ 
     return false; 
     alert('Passwords are not the same.'); 
    } 
    else{ 
     return true; alert('work'); 
    } 
} 
+0

Thanks :))))))))) –

0

你只是聲明瞭函數checkIt。你需要調用該函數來實際檢查它。

+0

他在這裏調用它:'

' – Barmar

+0

是的,你說得對。我錯過了。 – Samuel

+0

如果它不相關,你應該刪除你的答案。 – Barmar