首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > JavaScript >

JS代码执行顺序有关问题

2013-01-07 
JS代码执行顺序问题本帖最后由 qq3079530 于 2012-12-25 17:35:31 编辑【代码略有删除,但和问题相关的都没

JS代码执行顺序问题
本帖最后由 qq3079530 于 2012-12-25 17:35:31 编辑 【代码略有删除,但和问题相关的都没删】


<script type="text/javascript">
var StudentInfoList = new Array();
function SearchStudent() {
var tbSelectStudent = document.getElementById("tbSelectStudent");
if (tbSelectStudent.rows.length > 1) {
for (var i = 0; i < tbSelectStudent.rows.length; i++) {
tbSelectStudent.deleteRow(1);
}
}
$.post("xxx.ashx", { "func": "ffff", "t": escape(telNum) }, function (resultJSON) {
if (resultJSON == "") {
$.dialog.tips("未查询到数据,请检查搜索条件是否正确!", 2, "hits.png");
$("#divSelect").hide();
} else {
StudentInfoList = eval(resultJSON);
if (StudentInfoList.length == 1) {
$.dialog.tips("正在查询学生信息,请稍后...", 1, "loading.gif", function () {
showStudentInfo(0);
});
$("#divSelect").hide();
} else {
$.dialog.tips("正在查询学生信息,请稍后...", 1, "loading.gif", function () {
var newRowIndex = 1;
$.each(StudentInfoList, function () {
var newTableRow = tbSelectStudent.insertRow(newRowIndex);
var newCell0 = tbSelectStudent.rows[newRowIndex].insertCell(0);
newCell0.innerHTML = this.SchoolName;
var newCell1 = tbSelectStudent.rows[newRowIndex].insertCell(1);
newCell1.innerHTML = this.ClassName;
var newCell2 = tbSelectStudent.rows[newRowIndex].insertCell(2);
newCell2.innerHTML = this.StudentName;
var newCell3 = tbSelectStudent.rows[newRowIndex].insertCell(3);
newCell3.innerHTML = this.TelNum;
var newCell4 = tbSelectStudent.rows[newRowIndex].insertCell(4);
newCell4.innerHTML = this.Flag;
var newCell5 = tbSelectStudent.rows[newRowIndex].insertCell(5);
newCell5.innerHTML = this.CardCode;
var newCell6 = tbSelectStudent.rows[newRowIndex].insertCell(6);
newCell6.innerHTML = "<a href="javascript:void(0)" onclick="showStudentInfo(" + (newRowIndex - 1) + ");">选择</a>";
newRowIndex++;
});
var currentcolor = "";
$("#tbSelectStudent").find("tr").mouseover(function () {
currentcolor = this.style.backgroundColor;
this.style.backgroundColor = "#D4EBFF";
});
$("#tbSelectStudent").find("tr").mouseout(function () {
this.style.backgroundColor = currentcolor;
});
});
$("#divSelect").show();
}
}
});
}
</script>


问题大概是这样的:
第一次点击按钮js动态加载到表格Table里面100条数据。
第二次点击按钮,先删除Table中的所有行,然后将查到的数据重新加载到Table中。
这时在页面看Table中原有的100条数据并没有删除,将查出来的数据又重新加载到了Table中。
但是点击第一次加载的100行中的“选择”按钮,提示NULL,就是说100行数据已经删除了,但在页面还能看到,实际已经不存在了。

初步判定应该是js的执行顺序问题,但不知道怎么解决!
求大神!!!
[解决办法]
?for?(var?i?=?0;?i?<?tbSelectStudent.rows.length;?i++)?{
????????????????tbSelectStudent.deleteRow(1);
????????????}

这块有问题
不若
while(tbSelectStudent.length>1)
{
????tbSelectStudent.deleteRow(1);


}


[解决办法]
貌似用到ajax的样子 
这样的话在ajax返回值前就往下运行程序了

所以得到的值可能不对
[解决办法]
引用:
引用:不好意思,是这块
JavaScript code?123            for (var i = 0; i < tbSelectStudent.rows.length; i++) {                tbSelectStudent.deleteRow(1);            }
换成
JavaSc……
你什么浏览器?我这里的测试是后续的insertRow()、insertCell()都正常。注意,里面的索引参数要从0开始,你代码里是从1开始,是不对的。

热点排行