2005年的时候开发了一个WEB应用程序,前端技术用了自己写的javascript脚本。我用javascript脚本动态地向table中增加行,每行里面有一些input框,例如:id[]、name[]、price[]等等。程序当时在ie6,firefox下都调试正常,所有程序开发完后经过3个月测试,也没发现任何问题。所以将软件进行推广,在集团内40多个机构使用。半年左右发现某些机构中的数据出现错乱,反复查找程序都没有找到什么问题。情况如下:例如一组这样的数据:
id[] name[] price[]
22 西瓜 1.5
25 东瓜 1.2
29 南瓜 0.8
38 北瓜 1.9我会将这4组数据插入到数据库,我用自己的电脑怎么都没出现过问题。不过客户的电脑插入到数据库后常常出现这样的情况:id[] name[] price[]
25 西瓜 1.5
22 东瓜 1.2要么id错位,要么名字或价格错位。不知道是javascript的问题,还是浏览器问题。还是在用php程序得到表单数据的时候有问题。为什么偶尔会错位,只是偶尔。为什么有些客户端运行三、四年都没出现过一次这样的问题,为什么有些客户端常常出问题。可能出现的问题会是在哪里?

解决方案 »

  1.   

    可以说是自己的数据库,服务器是自己买的,自己安装的。windows 2000。不过数据库的版本比较老,是mysql4.03真不知道是什么问题了。最近准备全部重写这是我的代码,好几年前写的了。当时ajax并不流行,prototype、jquery等js库也没听说过。
    /** Add a line of depotin table. */
    function add_item(obj,article_id,article_coding,article_name,parent_id,articlecat_langname,article_size,article_unit,frm,amount,max_price)
    {
    var row,cell,str;
    row = eval("window.parent.parent.document.all["+'"'+obj+'"'+"]").insertRow(-1);
    if(row != null )
    {
    cell = row.insertCell(0);
    str="<input type='hidden' name='article_id[]' value='"+article_id+"'><input type='hidden' name='max_price[]' value='"+max_price+"'><input type='text' style='width:65px' name='article_coding[]' value='"+article_coding+"' class='disabled' readonly>"
    cell.innerHTML=str; cell = row.insertCell(1);
    str="<input type='text' style='width:70px' name='article_name[]' value='"+article_name+"' class='disabled' readonly>"
    cell.innerHTML=str; cell = row.insertCell(2);
    str="<input type='hidden' name='parent_id[]' value='"+parent_id+"'><input type='text' style='width:65px' name='articlecat_langname[]' value='"+articlecat_langname+"' class='disabled' readonly>"
    cell.innerHTML=str; cell = row.insertCell(3);
    str="<input type='text' style='width:65px' name='article_size[]' value='"+article_size+"' class='disabled' readonly>"
    cell.innerHTML=str; cell = row.insertCell(4);
    str="<input type='text' style='width:68px' name='article_productiondate[]' value=''>"
    cell.innerHTML=str; cell = row.insertCell(5);
    str="<input type='text' style='width:40px' name='article_durability[]' value=''>"
    cell.innerHTML=str; cell = row.insertCell(6);
    str="<input type='text' style='width:40px' name='article_unit[]' value='"+article_unit+"' class='disabled' readonly>"
    cell.innerHTML=str; cell = row.insertCell(7);
    str="<input type='text' style='width:40px' name='article_number[]' autocomplete='off' onFocus='this.select()' onkeydown=doKeyDown(event,'"+frm+"',this) onKeyUp=count_price('"+frm+"',this,'"+amount+"');>"
    cell.innerHTML=str; cell = row.insertCell(8);
    str="<input type='text' style='width:40px' name='article_price[]' autocomplete='off' onKeyUp=count_price('"+frm+"',this,'"+amount+"');>"
    cell.innerHTML=str; cell = row.insertCell(9);
    str="<input type='text' style='width:50px' name='article_amount[]' autocomplete='off' value='0.00' onblur='javascript:this.value=Math.round(parseFloat(this.value)*100)/100' onKeyUp=count_amount('"+frm+"','"+amount+"',this);>"
    cell.innerHTML=str; cell = row.insertCell(10);
    str="<input type='text' style='width:65px' name='article_re[]'><input type='checkbox' name='[]' value='"+article_id+"' class='checkbox' />"
    cell.innerHTML=str;
    }
    }
      

  2.   

    有用到删除和批量删除的js,问题真的出现在这里?
    /** Delete a line of table. */
    function delete_item(id, name, obj, amount)
    {
    var parent = document.getElementById(id);
    if (!parent)
    {
    return;
    } var rb = parent.getElementsByTagName('input'); var rowNum,curRow;
    for (var r = 0; r < rb.length; r++)
    {
    if (rb[r].name.substr(0, name.length) == name)
    {
    if (rb[r].checked == true)
    {
    curRow = rb[r].parentNode.parentNode;
    rowNum = eval("document.all."+obj).rows.length - 1;
    eval("document.all["+'"'+obj+'"'+"]").deleteRow(curRow.rowIndex);
    }
    }
    }
    count_amount(id, amount,'0');
    }
      

  3.   

    不是很清楚,客户端的浏览器以及浏览器设置都不是很清楚。应该是 ie 6,javascript没有禁止。再说了客户端并不是每次都出现这样的情况。我自己每次调试都是正确的,我换过不同的环境测试过。3楼或许也出现过这样的情况
      

  4.   

    好长...通常是index删掉了...但是其中有一项残留了点信息...结果后面加的时候又用上了...
      

  5.   

    应该是删除的时候出问题了.
    if (rb[r].name.substr(0, name.length) == name)
    判断这个的时候name是唯一的吗?如果出现重名会怎样?
      

  6.   

    name有可能会重名,不过重名也可以正常删除。并且表格内容也显示正常
      

  7.   

    比如有下列情况:
    ID NAME PRICE
    22 西瓜  1.2
    23 西瓜  1.3
    那你只删除其中PRICE为1.2的西瓜时不是就出错了吗?
      

  8.   


    不会出现这种情况,id 和 name不能都一样的。有如下的情况:
    ID NAME PRICE
    22 西瓜  1.2
    22 西瓜  1.2 复选框选中的才会被删除
      

  9.   

    下次不用insertRow这样的玩意了。
      

  10.   

    应该不关 insertRow的事吧。//我之前也是用这个方法。兼容还行,不过再有这种应用的话也应该不会用这个了。