document.getElementById无法更新文本框值


document.getElementById fails to update textbox value

我有一个生成表行的php代码:

    <table id="listOfProducts" name="listOfProducts" width="100%">
    <tr>
    <th>Item</th>
    <th>Product</th>
    <th>Description</th>
    <th>Unit Price</th>
    <th>Select</th>
    <th>Qty</th>
    <th>Amount</th>
    </tr>
    Products on List: 2
    <tr class="row1" id="tr1">
    <td id="pid1">1</td>
    <td id="pname1">Paint Brush</td>
    <td id="pdesc1">Premium Brand </td>
    <td id="pprice1">24</td>
    <td><input type="checkbox" id="chk1" checked></td>
    <td><input type="text" style="width:50px;" onkeyup="getall('tr1','ttp1')" id="1"/></td>
    <td><input type="text" id="ttp1" name="ttp1" value="00"/></td>
    </tr>
    <tr class="row1" id="tr2">
    <td id="pid2">2</td>
    <td id="pname2">Iron Nails</td>
    <td id="pdesc2">Stainless</td>
    <td id="pprice2">50</td>
    <td><input type="checkbox" id="chk2" checked></td>
    <td><input type="text" style="width:50px;" onkeyup="getall('tr2','ttp2')" id="2"/></td>
    <td><input type="text" id="ttp2" name="ttp2" value="00"/></td>
    </tr>
    </table>

这成功地加载了表中的值,并在调用onkeyup函数后调用javascript getall。

这是javascript代码

    function getall( strId, prdno){
var tmpObj = document.getElementById(strId);
var myobj = $("#listOfProducts").find("tr#" + strId);
     alert("reported id for testing: " + tmpObj.getAttribute("id"));
 //alert (prdno);
    var prdid= myobj.find('td:eq(0)').text();
    var prdname= myobj.find('td:eq(1)').text();
    var prddesc= myobj.find('td:eq(2)').text();
var prduprice = parseFloat(myobj.find('td:eq(3)').text());
var quan = myobj.find('td:eq(5) input').val();
var ans=prduprice * quan;
document.getElementById(prdno).value=ans;       
    }

我不知道真正的问题是什么。我检查了包含文本框的id是否成功传递,并且它是…

您可以尝试使用jquery来更新它:$('#'+prdno).val(ans);

我只是复制并粘贴了你的代码,并在顶部添加了jquery include(v1.7.2),它完全适用于Opera、FF和Chrome