【发布时间】:2014-01-23 20:30:20
【问题描述】:
我正在尝试通过我自己的两个字段进行验证。当它们为空时,我想显示一条消息。
问题是“如果”条件没有正确执行,我不知道为什么。
这是 HTML 代码:
<div id="switchingform">
<label for="currentsupplier">Who is your current supplier?</label>
<input type="text" name="currentsupplier" id="currentsupplier"/><br />
<span class="validateText" id="validateSupplier"><p>Please enter your current supplier</p></span>
<label for="postcode">What is your post code?</label>
<input type="text" name="postcode" id="postcode"/><br />
<span class="validateText" id="validatePostCode"><p>Please enter your post code</p></span>
</div>
这是 JQUERY 代码:
$("#telecomBusNext").click(function(){
if($("#currentsupplier").val().length < 1 || $("#postcode").val().length < 1) {
$("#validateSupplier").fadeIn(400);
$("#validatePostCode").fadeIn(400);
return false;
} else if($("#currentsupplier").val().length > 1 || $("#postcode").val().length < 1) {
$("#validatePostCode").fadeIn(400);
$("#validateSupplier").fadeOut(400);
return false;
} else if($("#currentsupplier").val().length < 1 || $("#postcode").val().length > 1) {
$("#validatePostCode").fadeOut(400);
$("#validateSupplier").fadeIn(400);
return false;
} else {
$("#validateSupplier").fadeOut(400);
$("#validatePostCode").fadeOut(400);
}
});
提前致谢。
【问题讨论】:
-
你能定义“不工作”吗?
-
将
||更改为&&,它应该可以工作。您在两个不同的条件中定义了两个相同的条件。您的逻辑是错误的。 -
你显然不明白
||和&&之间的区别。这对程序员来说是一个非常基本的误解。 -
你检查长度小于 1 和长度大于 1。那么长度正好等于 1 呢?
-
巴尔玛,你是对的!我犯了一个愚蠢的错误。谢谢!
标签: jquery validation if-statement conditional-statements