Jquery 如何打破美元每
我使用$。每个jquery如下Jquery 如何打破美元每,jquery,webmethod,Jquery,Webmethod,我使用$。每个jquery如下 $.each($('.FormContainer div.MainDiv'), function () { $.ajax({ type: "POST", url: pageUrl + '/SaveFormResponse', data: jsonText, contentType: "application/json; charset=utf-8", dataType: "json", suc
$.each($('.FormContainer div.MainDiv'), function () {
$.ajax({
type: "POST",
url: pageUrl + '/SaveFormResponse',
data: jsonText,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
if (isNaN(response.d) == false) {
//continue executing $.each further
}
else {
alert(response.d);
//stop executing $.each further in case of error
}
},
failure: function (response) {
alert(response.d);
}
});
});
现在的问题是,在出现错误的情况下,我希望$.each停止进一步执行,但它不会停止,直到它不会遍历所有div
我还尝试维护一个变量,在每次迭代时检查它的值,并在错误时设置它的值,但当
$时仍然不起作用。ajax
给您一个结果,$。每个都已经完成了。注意,ajax中的第一个“a”表示异步。您可以做的是创建一个递归函数(您没有使用每个元素的任何特定内容,但我假设您这样做,我使用了$elem
):
要中断$.each(),需要返回false
$(function() {
var temp = [0, 1, 2, 3, 4, 5];
$.each(temp, function(k, v) {
$('body').append(v);
if (v == 3) {
return false;
}
});
});
输出:
0123
但是,如果您想像这样使用异步$.ajax()调用,则需要在回调函数中决定是继续执行下一项还是停止
var currentItem = 0, itemSet = $('.FormContainer div.MainDiv');
function workOnItem(itemNum) {
if (itemSet[itemNum] != undefined) {
var item = itemSet[itemNum];
$.ajax({
type: "POST",
url: pageUrl + '/SaveFormResponse',
data: jsonText,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
if (isNaN(response.d) == false) {
//continue executing $.each further
currentItem++;
workOnItem(currentItem);
}
else {
alert(response.d);
//stop executing $.each further in case of error
}
},
failure: function (response) {
alert(response.d);
}
});
}
}
workOnItem(0);
我还没有测试过它,但我猜类似这样的东西可以在没有递归或同步行为的情况下完成:
var XHRarray = [];
$.each($('.FormContainer div.MainDiv'), function() {
var XHR = $.ajax({
type: "POST",
url: pageUrl + '/SaveFormResponse',
data: jsonText,
contentType: "application/json; charset=utf-8",
dataType: "json"
}).done(function(response) {
if (isInteger(response.d)) {
//continue executing $.each further
}else{
stopAll(); //stop all pending ajax requests
}
}).fail(function() {
stopAll(); //stop all pending ajax requests
});
XHRarray.push(XHR);
});
function stopAll() {
$.each(XHRarray, function(i,e) {
e.abort();
}
}
function isInteger(s){ //checks string for numbers only
for (var i = 0; i < s.length; i++){
var c = s.charAt(i);
if (((c < "0") || (c > "9"))) return false;
}
return true;
}
var xhraray=[];
$.each($('.FormContainer div.MainDiv'),函数(){
var XHR=$.ajax({
类型:“POST”,
url:pageUrl+'/SaveFormResponse',
数据:jsonText,
contentType:“应用程序/json;字符集=utf-8”,
数据类型:“json”
}).完成(功能(响应){
如果(isInteger(答复d)){
//继续执行$。再进一步
}否则{
stopAll();//停止所有挂起的ajax请求
}
}).fail(函数(){
stopAll();//停止所有挂起的ajax请求
});
推送(XHR);
});
函数stopAll(){
$.each(xharray,function(i,e){
e、 中止();
}
}
函数isInteger{//只检查字符串中的数字
对于(var i=0;i“9”))返回false;
}
返回true;
}
Ajax是异步的,所以在回调中中断循环对您没有任何好处,因为循环已经完成很久了。使用承诺和不同的对象。您确定有一个失败
处理程序吗?为什么您要迭代一组div,但没有从中检索任何信息?我在那里做了很多工作,但没有发布她的消息e因为代码太多了,但本质上你是根据每个div的内容发出请求的,如果有人给出了无效的响应,你想中止进一步的请求吗?@rahul你在尝试什么?记住,你需要在$中返回false
。each()
上下文,而不是在$.ajax()中
你认为这在异步方法中是如何工作的???@adeneo你只需阅读问题的标题。这是正确的答案。如果这个人会改进他的问题,我会改进我的答案。
var XHRarray = [];
$.each($('.FormContainer div.MainDiv'), function() {
var XHR = $.ajax({
type: "POST",
url: pageUrl + '/SaveFormResponse',
data: jsonText,
contentType: "application/json; charset=utf-8",
dataType: "json"
}).done(function(response) {
if (isInteger(response.d)) {
//continue executing $.each further
}else{
stopAll(); //stop all pending ajax requests
}
}).fail(function() {
stopAll(); //stop all pending ajax requests
});
XHRarray.push(XHR);
});
function stopAll() {
$.each(XHRarray, function(i,e) {
e.abort();
}
}
function isInteger(s){ //checks string for numbers only
for (var i = 0; i < s.length; i++){
var c = s.charAt(i);
if (((c < "0") || (c > "9"))) return false;
}
return true;
}