JavaScript - 禁用提交按钮

时间:2018-05-05 10:53:01

标签: javascript submit getelementbyid

我的功能无法禁用提交按钮(id =“mySubmit”),其余功能正常。 (该函数在“onkeyup”上调用)

如果两个密码字段的值不匹配,我试图禁用它。

function check_match() {

    var pw1 = document.getElementById("one").value;
    var pw2 = document.getElementById("two").value;

    if (pw1 == pw2) {
        document.getElementById("error_match").style.color = "green";
        document.getElementById("error_match").innerHTML = "paswords match";
        document.getElementById("mySubmit").disabled = false;
    } else {
        document.getElementById("error_match").style.color = "red";
        document.getElementById("error_match").innerHTML = "passwords do not match";
        document.getElementById("mySubmit").disabled = true;
    }

}
<form action="#" class="userregister" method="POST">

    <label for="email"><b>E-Mail</b></label>
    <input type="email" placeholder="Enter E-Mail" name="email" required />

    <!--Password1-->
    <label for="password"><b>Passwort</b></label>
    <input type="password" placeholder="Enter Password" name="password" id="one" pattern="(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,}" onkeyup="check_match();" required />
    <span id="error"></span>

    <!--Password2-->
    <label for="password2"><b>repeat Password</b></label>
    <input type="password" placeholder="Enter Password again" name="password2" id="two" onkeyup="check_match();" required />
    <span id="error_match"></span><br />

    <input type="checkbox" id="conditionbox" onclick="conditioncheck()" checked required>
    <a href="conditions.php" style="text-decoration: none">conditions </a>accepted<br />
    <span id="message" class="agberror"></span>

    <button type="submit" id="mySubmit" name="reg">Register</button>

</form>

我将脚本部分放在body标签的末尾

1 个答案:

答案 0 :(得分:0)

您只需使用===而不是==。

3 =='3'// true - 因为值相同 3 ==='3'// false - 因为数据类型不相同

始终使用===严格等于