JS doesn't get value from input

喜夏-厌秋 提交于 2019-12-12 03:36:27

问题


The problem is that I can't get value of my password fields - alert with pso1 returns null.
I don't see any error in this code so I'm asking for help.

<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>

回答1:


In your code you are setting the values of pso1 and ps02 once right after the page load before their values are changed. You will want to update these values instead, declared them as local variables inside your function:

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');
    }
}



回答2:


You have only declared the function checkIt. You need to call that function to actually check it.



来源:https://stackoverflow.com/questions/39540833/js-doesnt-get-value-from-input

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!