日期:2014-05-16  浏览次数:20351 次

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(tbSelect