我想通过Ajax根据用户选择的栏目ID生成页面,ID为多个
然后一个一个的传递到后台.ashx生成静态页面,把成功的值比如1传会前台,判断为1成功了之后再进行下一个ID,请问如何进行循环?贴出部分代码,等待补充
var WebIDList = WebIDListStr.split(","); $.ajax({
url: "CreateHTML.ashx?action=createindex",
type: "post",
data: { "webid": WebIDList[0] }, //提交表单
datatype: "text",
async: true,
success: function (data) {
if (data == "1") {
//成功,进行下一个,WebIDList[1]
}
},
});只要解答就行了,不要问为何不一下把值传过去,再后台生成,因为一下子传过去,生成就会超时
使用async: false,可以实现我说的循环,但是浏览器没有相应。
请问既能实现循环,又能使浏览器相应该如何解决?
然后一个一个的传递到后台.ashx生成静态页面,把成功的值比如1传会前台,判断为1成功了之后再进行下一个ID,请问如何进行循环?贴出部分代码,等待补充
var WebIDList = WebIDListStr.split(","); $.ajax({
url: "CreateHTML.ashx?action=createindex",
type: "post",
data: { "webid": WebIDList[0] }, //提交表单
datatype: "text",
async: true,
success: function (data) {
if (data == "1") {
//成功,进行下一个,WebIDList[1]
}
},
});只要解答就行了,不要问为何不一下把值传过去,再后台生成,因为一下子传过去,生成就会超时
使用async: false,可以实现我说的循环,但是浏览器没有相应。
请问既能实现循环,又能使浏览器相应该如何解决?
var WebIDList = WebIDListStr.split(",");
for(var i=0;i<WebIDList.length;i++){
$.ajax({
url: "CreateHTML.ashx?action=createindex",
type: "post",
data: { "webid": WebIDList[0] }, //提交表单
datatype: "text",
async: true,
success: function (data) {
if (data == "1") {
continue;
}else{
break;
}
},
});
}
var index=0;
send();
function send(){
if(index==WebIDList.length){
alert("ok");
return;
}
$.ajax({
url: "CreateHTML.ashx?action=createindex",
type: "post",
data: { "webid": WebIDList[index] }, //提交表单
datatype: "text",
async: true,
success: function (data) {
if (data == "1") {
//成功,进行下一个,WebIDList[1]
index++;
send();
}
},
});
}
break 和 continue 不能位于循环外。hch126163,不能继续执行吧。
看着个帖子 http://topic.csdn.net/u/20110228/16/36f298c2-cb92-48f9-9237-d6627fb7346f.html
wern0565
,您的方法貌似可行
var WebIDList = WebIDListStr.split(",");function run(index){
$.ajax({
url: "CreateHTML.ashx?action=createindex",
type: "post",
data: { "webid": WebIDList[index] }, //提交表单
datatype: "text",
async: true,
success: function (data) {
if (data == "1") {
//成功,进行下一个,WebIDList[1]
index++;
if(WebIDList.length>index){run(index);}
}
},
});
}
if(WebIDList.lenght>0){
run(0);
}
但是我觉得判断循环最好以$.ajax(内部的回调函数的值来判断是否继续循环,这样显得比较真实一点!不知道实际是否如此!
var WebIDList = WebIDListStr.split(",");
send(WebIDList);
function send(ids) {
if (!ids) return;
if (ids.length == 0) return;
var id = ids.pop();
$.ajax( {
url : "CreateHTML.ashx?action=createindex",
type : "post",
data : { "webid" : id }, //提交表单
datatype : "text",
async : true,
success : function(data) {
if (data == "1") {
send(ids);
}
}
});
}