如何在javascript中运行命令脚本 [英] how run order script in javascript

查看:61
本文介绍了如何在javascript中运行命令脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

hi
我想为所有网格行运行IsValidHesNo方法。但它在循环中运行最后'i'。

plz帮助我

hanks很多



< pre lang =Javascript> var rows = document .getElementById( <%= grdSnd.ClientID%>)。rows;
for var i = 1 ; i< rows.length; i ++){
hesCode = rows [i] .cells [ 0 ]。getElementsByTagName( input)[ 0 ]。value + rows [i ] .cells [ 1 ]。getElementsByTagName( input )[ 0 ]。value + rows [i] .cells [ 2 ]。getElementsByTagName ( input)[ 0 ] .value
IsValidHesNo(hesCode, function (res){
alert(res);
});
}
function IsValidHesNo(hesCode,callBack){
$ .ajax({
type: post
contentType: < span class =code-string> application / json; charset = utf-8,
data: {'hesCode':' + hesCode + '}
url: insertSanad.aspx / IsValidHesNo
dataType: json
成功: function (响应){
callBack(response.d);
},
错误: function (){
callBack( );
}
});
}

解决方案

.ajax({
type: post
contentType: application / json; charset = utf-8
data: {' hesCode':' + hesCode + '}
url : insertSanad.aspx / IsValidHesNo
dataType: json
成功: function (响应){
callBack(response.d);
},
错误: function (){
callBack( );
}
});
}


这是因为ajax调用是异步的。

你的可变的hesCode是一个全局变量。所以hescode将是window.hesCode的引用。你应该使用一个闭包。



如果你把包含循环的第一部分放在一个匿名函数中并声明hesCode前面有一个var它应该可以工作。 / BLOCKQUOTE>

hi I want run IsValidHesNo method for all of rows of grid. but it is run for last 'i' in loop.
plz help me
hanks alot

var rows = document.getElementById("<%= grdSnd.ClientID %>").rows;
for (var i = 1; i < rows.length; i++) {
hesCode = rows[i].cells[0].getElementsByTagName("input")[0].value + rows[i].cells[1].getElementsByTagName("input")[0].value + rows[i].cells[2].getElementsByTagName("input")[0].value
 IsValidHesNo(hesCode, function (res) {
alert(res);
});
}
function IsValidHesNo(hesCode, callBack) {
       $.ajax({
           type: "post",
           contentType: "application/json; charset=utf-8",
           data: "{'hesCode':'" + hesCode + "'}",
           url: "insertSanad.aspx/IsValidHesNo",
           dataType: "json",
           success: function (response) {
               callBack(response.d);
           },
           Error: function () {
               callBack("");
           }
       });
   }

解决方案

.ajax({ type: "post", contentType: "application/json; charset=utf-8", data: "{'hesCode':'" + hesCode + "'}", url: "insertSanad.aspx/IsValidHesNo", dataType: "json", success: function (response) { callBack(response.d); }, Error: function () { callBack(""); } }); }


This caused because the ajax call is asynchronous.
And your varable hesCode is a global variable. So hescode will be a reference to the window.hesCode. You should use a closure.

If you put the first part containing the loop in an anonymous function and declare hesCode with a var in front of it it should work.


这篇关于如何在javascript中运行命令脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆