JS无法从输入中获取价值 [英] JS doesn't get value from input

查看:76
本文介绍了JS无法从输入中获取价值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题是我无法获取密码字段的值-pso1的警报返回空值.
我没有在此代码中看到任何错误,所以我正在寻求帮助.

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>

推荐答案

在代码中,页面加载后立即设置pso1ps02的值,然后更改它们的值.您将需要更新这些值,将它们声明为函数内的局部变量:

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

这篇关于JS无法从输入中获取价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆