按提交后如何不刷新

时间:2016-04-03 20:44:55

标签: javascript html

我正在处理注册页面(表单)。如果两个密码字段不匹配,我想显示警报但我不希望在显示警报后刷新它并丢失所有其他字段中的所有信息。理想情况下,如果它仅重置两个密码字段并保持其他所有字段相同,我会喜欢它。

JavaScript代码:

function passwordMatch(){

    if(document.getElementById('password1').value !== document.getElementById('password2').value){
        alert("Passwords don't match!");
        false;
    }

}

我环顾四周,所有我能找到的是使用PHP的解决方案我不确定是否应该切换到那个但我更喜欢使用JavaScript。

HTML代码

<form id = "testform" name = "testform" method = "post" action="" onsubmit="return validate(this);">
    <p><label for="userFirstName">Fisrt Name:</label>
        <input name = "userFirstName" type = "text" id = "userFirstName"/>
    </p>
    <p><label for ="userLastName">Last Name:</label>
        <input name="userLastName" type = "text" id = "userLastName"/>
    </p>
    <p><label for = "userName">Username:</label>
        <input name = "userName" type = "text" id = "userName"/>
    </p>
    <p><label for = "password">Enter Password:</label>
        <input name = "password" type = "password" id = "password1"/>
    </p>
    <p><label for = "password2">Re-Enter Password:</label>
        <input name = "password2" type = "password" id = "password2"/>
    </p>
    <p>
        <input type = "submit" name="save" href = "login.php"  value = "Register"/>
    </p>
</form>

0 个答案:

没有答案