Javascript函数未修改html
Javascript function not modifying html
我有一个Javascript函数的问题。它应该使用if
语句来检查表单中的两个输入框是否具有相同的值。
HTML:
<form action="confirm_account.php" method="post" id="form" onsubmit="checkpassword()">
<p style="font-family:latine;">Password: <input type="password" name="password" id="password" style="font-family:latine;" required>
</p>
<br><br>
<p style="font-family:latine;">Re-enter Password: <input type="password" name="password-reenter" id="password-reenter" style="font-family:latine;" required> </p>
<span id="password-warning" style="color:red; font-weight:bold;"></span>
Javascript:
var password = document.getElementById("password").value;
var password2 = document.getElementById("password-reenter").value;
function checkpassword(){
if('password' == 'password2'){
document.getElementById("form").action = "confirm_account.php";
document.getElementById("password-warning").innerHTML = "";
}
else{
document.getElementById("password-warning").innerHTML = "The passwords do not match, please re-enter the password.";
document.getElementById("form").onsubmit = "return false";
}
};
此时会显示警告消息,但onsubmit
未被修改。
试试这个,它应该可以工作:
function checkpassword() {
var password = document.getElementById("password").value;
var password2 = document.getElementById("password-reenter").value;
if (password === password2) {
document.getElementById("form").action = "confirm_account.php";
document.getElementById("password-warning").innerHTML = "";
document.getElementById("password-success").innerHTML = "The passwords match! Well done!";
} else {
document.getElementById("password-warning").innerHTML = "The passwords do not match, please re-enter the password.";
document.getElementById("password-success").innerHTML = "";
document.getElementById("password-reenter").focus();
return false;
}
}
<form action="#" method="post" id="form" onsubmit="return checkpassword()">
<p style="font-family:latine;">Password:
<input type="password" name="password" id="password" style="font-family:latine;" required>
</p>
<br>
<p style="font-family:latine;">Re-enter Password:
<input type="password" name="password-reenter" id="password-reenter" style="font-family:latine;" required>
</p>
<p>
<span id="password-success" style="color:green;"></span>
<span id="password-warning" style="color:red; "></span>
<input type="submit" name="btnSave" value="Try it">
</form>
var password = document.getElementById("password").value;
var password2 = document.getElementById("password-reenter").value;
在if
条件下,您不是在检查password
和password2
,而是在检查字符串。
应该是
if(password === password2){
而onsubmit
是一个事件,你不能给它分配字符串。因为你在表单的onsubmit中调用checkpassword
。为了防止表单提交,只需要return false
。
var password = document.getElementById("password").value;
var password2 = document.getElementById("password-reenter").value;
function checkpassword(){
if(password === password2){
document.getElementById("password-warning").innerHTML = "";
}
else{
document.getElementById("password-warning").innerHTML = "The passwords do not match, please re-enter the password.";
return false;
}
};
相关文章:
- Html未呈现图表
- Javascript函数未修改html
- 保留未修改的dom节点副本问题
- 使用Javascript打印HTML:未定义错误
- HTML未显示内容-最近工作
- Javascript/Html 未捕获的引用错误:赋值中的左侧无效;未捕获的引用错误:未定义计算
- 主干.js解析未修改的响应
- CSS 样式表值未修改
- HTML 未读取外部 Javascript 文件
- 页面视图不符合预期,HTML 未显示,页面为空白
- 通过javascript获取原始/未修改的请求字符串
- 插入的 HTML 未响应 jQuery
- 是否可以在单击 html 未显示列表中的项目标记时触发事件
- HTML:未提交文本输入
- 角度插入的HTML未附加到控制器
- 快速:在 304 上发送内容类型未修改
- 带有土耳其语文本的HTML未显示在UIWebview中
- HTML未调用外部JS文件
- JavaScript 304未修改问题
- 原始字符串是否始终保持未修改