<script language="JavaScript">
function validate(x) {
var cur_p = document.getElementById('current').value;
var new_p = document.getElementById('new').value;
var con_p = document.getElementById('confirm').value;
document.getElementById('msg_p').innerHTML = '';
document.getElementById('msg_cur').innerHTML = '';
if(x != cur_p)
{ document.getElementById('msg_cur').innerHTML = ' Your password was incorrect';
return false;
}
if(new_p != con_p)
{ document.getElementById('msg_p').innerHTML = 'Passwords do not match';
return false;
}
return (true);
}
</script>
HTML
<form action='change-password.php' method='post' onsubmit="return validate('<?=$current?>')" >
我的代码上有这些。 我无法同时显示这些ifs的结果。
if(x != cur_p)
and
if(new_p != con_p)
如果我放置
if(x != cur_p){}
位于
的顶部if(new_p != con_p){}
if(x!= cur_p)的响应结果将显示而后者不会
反之亦然。
我如何显示这两个ifs的结果 (假设那些条件满足)
答案 0 :(得分:3)
问题是你在第一个之后返回false
,所以第二个永远不会到达。相反,在每个中设置一个布尔变量并返回布尔变量(如果两者都失败则为true
,如果失败则为false
。)
// Boolean variable starts true, will get set to false if either condition is met:
var okFlag = true;
if(x != cur_p)
{ document.getElementById('msg_cur').innerHTML = ' Your password was incorrect';
// Set to false
okFlag = false;
}
if(new_p != con_p)
{ document.getElementById('msg_p').innerHTML = 'Passwords do not match';
// Set to false
okFlag = false;
}
// Return the flag, which is either true or false.
return okFlag;
答案 1 :(得分:2)
首先你的代码中有拼写错误
document.getElementById('msg_p').innerHTM = ''; <-- Missing an L
其次,当然如果你返回,它会退出该功能。所以代码不会执行这两个语句。
更改
if(x != cur_p)
{ document.getElementById('msg_cur').innerHTML = ' Your password was incorrect';
return false;
}
if(new_p != con_p)
{ document.getElementById('msg_p').innerHTML = 'Passwords do not match';
return false;
}
return (true);
到
var isValid = true;
if(x != cur_p)
{ document.getElementById('msg_cur').innerHTML = ' Your password was incorrect';
isValid = false;
}
if(new_p != con_p)
{ document.getElementById('msg_p').innerHTML = 'Passwords do not match';
isValid = false;
}
return isValid;