跳出 IF 语句 - EXIT;
Breaking out of IF Statement - EXIT;
我有这样的声明:
$("#submit").click(function(e) {
if (!confirm('Continue ?')) { return false; }
if (!$("#all").is(':checked')) {
$(".A").each(function() {
if($(this).val() == '0') { alert ('A - This Value Must Be Set'); return false; }
});
$(".B").each(function() {
if($(this).val() == '0') { alert ('B - This Value Must Also Be Set'); return false; }
});
$(".C").each(function() {
if ($(this).val().length < 1) { alert ('C - This Must Be Set'); return false; }
if (!$.isNumeric(this.value)) { alert ('C - Only Numeric Values'); return false; }
});
}
});
如果任何 class 'A' 的值为 0,那么我会收到警报:
A - This Value Must Be Set
这很好,但后来我接受了 class 'B' & class 'C' 检查。
我想要的是阻止所有额外检查是否发送了 A、B 和 C 警报。
我该怎么做?
我想使用 if 条件 else 来做到这一点。尝试这样的事情,
if (!$("#all").is(':checked')) {
var foundA = false;
var foundB = false;
var foundC = false;
$(".A, .B, .C").each(function() {
var self = $(this);
if($(this).val() == '0') {
if (self.prop('class') === 'A' && foundA === false){
foundA = true;
alert ('A - This Value Must Be Set');
} else if (self.prop('class') === 'B' && foundB === false){
foundB = true;
alert ('B - This Value Must Be Set');
} else if (self.prop('class') === 'c' && foundC === false){
foundC = true;
alert ('C - This Value Must Be Set');
}
}
});
}
尝试在 each 范围之外声明一个变量,如果设置了则退出函数:
$("#submit").click(function(e) {
if (!confirm('Continue ?')) { return false; }
var stopFunction = false;
if (!$("#all").is(':checked')) {
$(".A").each(function() {
if($(this).val() == '0') { alert ('A - This Value Must Be Set'); stopFunction = true; return false; }
});
if(stopFunction)
return false;
$(".B").each(function() {
if($(this).val() == '0') { alert ('B - This Value Must Also Be Set'); stopFunction = true; return false; }
});
if(stopFunction)
return false;
$(".C").each(function() {
if ($(this).val().length < 1) { alert ('C - This Must Be Set'); return false; }
if (!$.isNumeric(this.value)) { alert ('C - Only Numeric Values'); stopFunction = true; return false; }
});
if(stopFunction)
return false;
}
});
我有这样的声明:
$("#submit").click(function(e) {
if (!confirm('Continue ?')) { return false; }
if (!$("#all").is(':checked')) {
$(".A").each(function() {
if($(this).val() == '0') { alert ('A - This Value Must Be Set'); return false; }
});
$(".B").each(function() {
if($(this).val() == '0') { alert ('B - This Value Must Also Be Set'); return false; }
});
$(".C").each(function() {
if ($(this).val().length < 1) { alert ('C - This Must Be Set'); return false; }
if (!$.isNumeric(this.value)) { alert ('C - Only Numeric Values'); return false; }
});
}
});
如果任何 class 'A' 的值为 0,那么我会收到警报:
A - This Value Must Be Set
这很好,但后来我接受了 class 'B' & class 'C' 检查。 我想要的是阻止所有额外检查是否发送了 A、B 和 C 警报。
我该怎么做?
我想使用 if 条件 else 来做到这一点。尝试这样的事情,
if (!$("#all").is(':checked')) {
var foundA = false;
var foundB = false;
var foundC = false;
$(".A, .B, .C").each(function() {
var self = $(this);
if($(this).val() == '0') {
if (self.prop('class') === 'A' && foundA === false){
foundA = true;
alert ('A - This Value Must Be Set');
} else if (self.prop('class') === 'B' && foundB === false){
foundB = true;
alert ('B - This Value Must Be Set');
} else if (self.prop('class') === 'c' && foundC === false){
foundC = true;
alert ('C - This Value Must Be Set');
}
}
});
}
尝试在 each 范围之外声明一个变量,如果设置了则退出函数:
$("#submit").click(function(e) {
if (!confirm('Continue ?')) { return false; }
var stopFunction = false;
if (!$("#all").is(':checked')) {
$(".A").each(function() {
if($(this).val() == '0') { alert ('A - This Value Must Be Set'); stopFunction = true; return false; }
});
if(stopFunction)
return false;
$(".B").each(function() {
if($(this).val() == '0') { alert ('B - This Value Must Also Be Set'); stopFunction = true; return false; }
});
if(stopFunction)
return false;
$(".C").each(function() {
if ($(this).val().length < 1) { alert ('C - This Must Be Set'); return false; }
if (!$.isNumeric(this.value)) { alert ('C - Only Numeric Values'); stopFunction = true; return false; }
});
if(stopFunction)
return false;
}
});