我有这样的代码块,当代码找到一个设置为error的错误属性时,它应该阻止进一步执行,我该怎样阻止它执行:
$(document).ready(function() {
$("#SendMessage").click(function(e) {
var elements = $("#contact-area input:text[error=error]").get();
for (var i = 0; i < elements.length; i++) {
if ($(elements[0]).attr("error") == "error") {
alert("error found");
// Stop execution of whole function
}
}
$("#loaderImage").show();
$("#statusMessage").text("Sending, Please wait...");
window.setTimeout(function() { PageMethods.SendMessage(new Message().fullContent(), onSuccess(), onFailed()) }, "4000");
});
})
我怎么能用JQuery和Javascript做到这一点,也许有一个预定义的函数可以在JQuery中轻松完成。
答案 0 :(得分:2)
$(document).ready(function() {
var flag = false;
$("#SendMessage").click(function(e) {
var elements = $("#contact-area input:text[error=error]").get();
for (var i = 0; i < elements.length; i++) {
if ($(elements[0]).attr("error") == "error") {
alert("error found");
// Stop execution of whole function
flag = true;
break;
}
}
if (!flag){
$("#loaderImage").show();
$("#statusMessage").text("Sending, Please wait...");
window.setTimeout(function() { PageMethods.SendMessage(new Message().fullContent(), onSuccess(), onFailed()) }, "4000");
}
});
})
这会实现你所需要的吗?
答案 1 :(得分:1)
return;
$(document).ready(function() {
$("#SendMessage").click(function(e) {
var elements = $("#contact-area input:text[error=error]").get();
for (var i = 0; i < elements.length; i++) {
if ($(elements[0]).attr("error") == "error") {
alert("error found");
return;
}
}
$("#loaderImage").show();
$("#statusMessage").text("Sending, Please wait...");
window.setTimeout(function() { PageMethods.SendMessage(new Message().fullContent(), onSuccess(), onFailed()) }, "4000");
});
})