第一:我是JavaScript的新手。 所以..我有密码,重复密码,电子邮件和重复电子邮件字段的基本形式。我想检查密码是否等于重复密码。如果不是,则会显示警告消息并重新加载页面。相同的电子邮件和重复电子邮件 但是如果传递和重复密码不相等,并且电子邮件和重复电子邮件不相等,则会出现第一条警告消息,则第二条消息(此时用于电子邮件)显得过快。当两个字段不匹配时,我只想显示一条警报消息。单个else子句用于多个if子句 - javascript
<script type="text/javascript">
function checkFields() {
var pass= document.getElementById('password');
var reppass= document.getElementById('reppass');
var email= document.getElementById('email');
var repemail= document.getElementById('repemail');
if (pass.value != reppass.value) {
alert('Passwords dont match');
window.location.reload();
}
if (email.value != repemail.value) {
alert('Emails dont match');
window.location.reload();
}
else if (pass.value != reppass.value && email.value != repemail.value) {
alert('Both fields dont match');
window.location.reload();
}
}
</script>
而且形式:
<form onSubmit="checkFields()">
<p><label>Password:</label> <input name="password" id="password" required="true" type="password" /></p>
<p><label>Repeat password:</label> <input name="reppass" id="reppass" required="true" type="password" /></p>
<p><label>Email:</label> <input name="email" id="email" required="true" type="email" /></p>
<p><label>Repeat Email:</label> <input name="repemail" id="repemail" required="true" type="email" /></p>
<p><input type="submit" value="Send"></p>
</form>
这个答案的问题是,第三if语句将永远不会计算为true,因为你已经检查的值,然后返回。 – jacobsimeon
第三个陈述不应该先来吗?这样,如果发现两者不匹配,它会返回? – Simon
您正确更新了订单。 – topek