Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/448.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
通过id获取元素的javascript在一行上工作,但在另一行上不工作_Javascript - Fatal编程技术网

通过id获取元素的javascript在一行上工作,但在另一行上不工作

通过id获取元素的javascript在一行上工作,但在另一行上不工作,javascript,Javascript,我正在为视图中的挂起订单加载一个页面,并在一个表中显示数据,我正在通过javascript计算行总数,并且进展顺利,但当我尝试在页面中显示时,通过其id获取元素时出现错误,错误是:无法将属性“innerHTML”设置为null <div class="ziehharmonika"> <?php if( count($pending_orders) ): ?> <?php foreach ($pending_orders as $info): ?> <

我正在为视图中的挂起订单加载一个页面,并在一个表中显示数据,我正在通过javascript计算行总数,并且进展顺利,但当我尝试在页面中显示时,通过其id获取元素时出现错误,错误是:无法将属性“innerHTML”设置为null

 <div class="ziehharmonika">
 <?php if( count($pending_orders) ): ?>
 <?php foreach ($pending_orders as $info): ?>
<h3 id="header3" style="background-color: #00bad3; color:white;" 
    onclick="get_line_total(<?php print_r($info['id']);?>);"><span 
    style="color: black;">order# </span> <?php print_r($info['id']);?> 
    Status: </span> <span id="status<?php print_r($info['id']);?>"><?php 
    print_r($info['status']);?></span></h3>
<div class="w3_agile_recipe-grid"> 
    <section>
    <table cellpadding="0" cellspacing="0" class="ordertable" 
    id="ordertable<?php print_r($info['id']);?>">

      <tr>
        <th>Sr_No</th> <!-- Dummy cell for the row number and row commands 
        <th>Id</th>
        <th>item_Name</th>
        <th>price</th>
    <th>company</th>
        <th>quantity</th>
        <th>line_total</th>
      </tr>

<?php if( count($info['porders']) ): ?>
<?php

 $row_count = 1;
foreach ($info['porders']as $order_info){ ?> 
<tr>
        <td><?php echo $row_count; ?></td> <!-- Don't remove this column 
        as it's needed for the row commands -->
        <td class="ord_id"><?php print_r($order_info['id']);?></td>
    <td><?php print_r($order_info['name']);?></td>
        <td class="ord_price"><?php print_r($order_info['price']);?></td>
    <td><?php print_r($order_info['company']);?></td>
    <td class="ord_qty"><?php print_r($order_info['quantity']);?></td>
    <td class="line_total"></td>
      </tr>
   <?php $row_count++; }?>

    </table>
    <?php else: ?>
    <?php endif;?> 

<div><span>Total Amount</span><p id="tota<?php 
    print_r($order_info['id']);?>"></p></div>

    <div class="clearfix"></div>
    </section>
    </div>
    </div>


如果看不到JavaScript实际使用的呈现输出,那么查看php代码就毫无帮助。很抱歉,实际上问题出在span“tota”的id上。我用它来加数字,但事实上我不得不用它,所以当我用它的时候没有错误。我花了三天时间才得到答案,当我感到失望并打算放弃时,幸运的是我注意到了我的错误。
   var get_line_total = function(id){
   var table = document.getElementById('ordertable'+id);
   ord_price=0;
   ord_qty=0;
   linetotal=0;
    for (var r = 1, n = table.rows.length; r < n; r++) {

      ord_price = parseInt(table.rows[r].cells[3].innerHTML);
      ord_qty = parseInt(table.rows[r].cells[5].innerHTML);
      linetotal= ord_price * ord_qty;
  table.rows[r].cells[6].innerHTML = linetotal;
    }

    ord_total=0;
    for (var r = 1, n = table.rows.length; r < n; r++) {

      ord_total = ord_total + parseInt(table.rows[r].cells[6].innerHTML);

    }
    // this line is giving above said error
document.getElementById('tota'+id).innerHTML = ord_total;

    }