检查输入中的密码后激活按钮

时间:2017-09-15 09:05:51

标签: javascript html

我想创建一个表单,我输入密码和按钮,如果输入的密码正确,必须启用。

这是我已有的代码:

<form>
    <label for="password"><input type="password" name="password" size="15" id="password" onkeyup="activateBtn()" /></label>
    <input type="submit" id='delete' class='delete' disabled name="delete" value='Reset' onclick="resetFunc()" />
</form>

<script type="text/javascript">
    function activateBtn() {
       pass = document.getElementById('password').value;
        if (pass = "1917"){
            document.getElementById('password').onkeyup = function(){
                document.getElementById('delete').disabled = false;
        }
    }}
</script>
<script type="text/javascript">
    function resetFunc(){
        countDownDate = new Date(); localStorage.setItem('startDate', countDownDate);
    }
</script>

此时按钮会激活输入空间中键入的内容。

3 个答案:

答案 0 :(得分:0)

if (pass = "1917"){应为if (pass == "1917"){

答案 1 :(得分:0)

您使用了单个等于运算符。

请使用==代替&#39; =&#39;更新代码

&#13;
&#13;
function activateBtn() {
pass = document.getElementById('password').value;
console.log(pass);
document.getElementById('delete').disabled = pass !== '1917';
}
function resetFunc(){
countDownDate = new Date(); localStorage.setItem('startDate', countDownDate);
}
&#13;
<form>
  <label for="password"><input type="password" name="password" size="15" id="password" onkeyup="activateBtn()" /></label>
  <input type="submit" id='delete' class='delete' disabled name="delete" value='Reset' onclick="resetFunc()" />
</form>
&#13;
&#13;
&#13;

答案 2 :(得分:0)

代码if (pass = "1917")不是比较,它的设置。 (虽然这很简单。)

&#13;
&#13;
const txtPassword = document.getElementById('password');
const btnDelete = document.getElementById('delete');

txtPassword.addEventListener('keyup', (e) => {
	btnDelete.disabled = e.target.value !== '1917';
});
&#13;
<form>
  <label for="password">Password</label>
  <input id="password" type="password">
  <input id="delete" type="submit" value="Delete" disabled>
</form>
&#13;
&#13;
&#13;