简体   繁体   English

javascript不适用于php中动态添加的行

[英]javascript is not working for dynamically added rows in php

This is my html code. 这是我的html代码。 these rows are called dynamically 这些行被动态调用

<tr >
    <td><?php echo $row['qty'];?></td>
    <td class="record"><?php echo $row['prod_name'];?></td>
    <td><input type="text" id="price1" value="<?php echo 
    number_format($row['price'],2);?>" /></td>
    <td><input type="text" id="disc1" onFocus="startCalc();" 
    onBlur="stopCalc();" name="Discount"></td>
    <td><span id="tot1"><?php echo number_format($total,2);?></span></td>
    <td><a href="#updateordinance<?php echo $row['temp_trans_id'];?>" data-
   target="#updateordinance<?php echo $row['temp_trans_id'];?>" data-
   toggle="modal" style="color:#fff;" class="small-box-footer"><i 
   class="glyphicon glyphicon-edit text-blue"></i></a>

   <a href="#delete<?php echo $row['temp_trans_id'];?>" data-
   target="#delete<?php echo $row['temp_trans_id'];?>" data-toggle="modal" 
   style="color:#fff;" class="small-box-footer"><i class="glyphicon 
   glyphicon-trash text-red"></i></a>

  </td>
  </tr>

and this is my javascript code to caliculate discount for the products

function startCalc(){
    price = document.getElementById("price1").value;
    disc = document.getElementById("disc1").value;
    tot = parseInt(price) * parseInt(disc)/100;
    sub_tot = parseInt(price)-parseInt(tot);
    document.getElementById("tot1").innerHTML=sub_tot;

}

this function is working fine with first row . 此功能在第一行工作正常。 but second row onwards there is no action.i changed the id as class in my html code.but no luck.please check the code and give me a possible solution. 但是第二行开始没有动作。我在我的html代码中将id更改为类。但是没有运气。请检查代码并给我一个可能的解决方案。

Thanks in advance. 提前致谢。

If you change the HTML by removing the ID attributes ( which you don't need ) and assign input elements with a name attribute ( which they should have ) you can then alter the javascript function like this ( though not tested yet ) 如果您通过删除ID属性(不需要)来更改HTML,并为输入元素分配了name属性(它们应该具有),则可以像这样更改javascript函数(尽管尚未测试)

<tr>
    <!-- other html removed -->


    <td class="record"><?php echo $row['prod_name'];?></td>
    <td>
        <input type="text" name='price' value="<?php echo number_format($row['price'],2);?>" />
    </td>
    <td>
        <input type="text" name='disc' onFocus="startCalc(event);" onBlur="stopCalc(event);" name="Discount">
    </td>
    <td>
        <span data-name="tot">
        <?php echo number_format($total,2);?>
        </span>
    </td>


    <!-- other html removed -->

</tr>



function startCalc(event){
    var el=event.target;
    var parent=el.parentNode.parentNode;
    var price=parent.querySelectorAll('input[name="price"]')[0];
    var disc=parent.querySelectorAll('input[name="disc"]')[0];
    var out=parent.querySelectorAll('span[data-name="tot"]')[0];
    var tot=parseFloat( price.value ) * parseInt( disc.value );
    var subtot=parseFloat( price.value )-parseInt( tot );
    out.innerHTML=subtot;
}

The DOM Tree - parentNode &/or childNode DOM树-parentNode和/或childNode

在此处输入图片说明

To help you understand the concept of using node.parentNode.parentNode I hope the following might be of use. 为了帮助您理解使用node.parentNode.parentNode的概念,我希望以下内容可能有用。

In the above diagram you can see how the DOM elements relate to one another - for instance the branch on the left depicts an image within a table-cell ( td ), that table-cell is a child of the table-row ( tr ) which is itself a child of the table. 在上图中,您可以看到DOM元素如何相互关联-例如,左侧的分支描绘了一个表格单元格(td)中的图像,该表格单元格是表格行(tr)的子级这本身就是桌子的孩子。 So - going up the tree you are looking at the parentNode for the previous element and likewise going down the tree you are looking at the childNodes . 因此,沿着树,您正在查看上一个元素的parentNode ,同样,沿着树,您正在查看childNodes Understanding this enables you to traverse the DOM in the vertical direction though you can traverse sideways ( at certain points ) by using a sibling selector. 了解这一点使您可以在垂直方向上遍历DOM,尽管您可以使用sibling选择器进行横向遍历(在某些点上)。

In the function I posted yesterday the function does not utilise the ID of any input elements because of the need to have unique IDs so instead needs another mechanism to find the appropriate element in the DOM - in this case the el.parentNode.parentNode says "go up two levels in the DOM from this button" which equates to the table-row. 在我昨天发布的函数中,该函数未使用任何输入元素的ID,因为需要具有唯一的ID,因此需要另一种机制来在DOM中查找适当的元素-在这种情况下, el.parentNode.parentNode表示“从该按钮在DOM中上升了两个级别,这相当于表行。 From the tr node it then queries all nodes below to find the requested input elements & other elements using querySelectorAll 然后从tr节点查询以下所有节点,以使用querySelectorAll查找请求的输入元素和其他元素

To get the "Grand-Total" I would think you should use a global variable - declared when the page loads which is initially set to zero. 为了获得"Grand-Total"我认为您应该使用一个全局变量-在页面加载时声明,该变量最初设置为零。

<script>
    var grandtotal=0;

    function startCalc(event){
        var el=event.target;
        var parent=el.parentNode.parentNode;
        var price=parent.querySelectorAll('input[name="price"]')[0];
        var disc=parent.querySelectorAll('input[name="disc"]')[0];
        var out=parent.querySelectorAll('span[data-name="tot"]')[0];
        var tot=parseFloat( price.value ) * parseInt( disc.value );
        var subtot=parseFloat( price.value )-parseInt( tot );

        grandtotal+=subtot;

        out.innerHTML=subtot;
    }

</script>

It will be simple with jquery if you are already using it, 如果您已经在使用jquery,它将非常简单,

change id to class and you can write like this 将id更改为class,您可以这样编写

function startCalc(){
rowObj = $(this).closest('tr');
price = rowObj.find('.price1').val();
disc = rowObj.find('.disc1').val();
tot = parseInt(price) * parseInt(disc)/100;
sub_tot = parseInt(price)-parseInt(tot);
rowObj.find('.tot1').html(sub_tot);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM