【问题标题】:Getting id from append table and use it in arithmetic javascript从附加表中获取 id 并在算术 javascript 中使用它
【发布时间】:2019-03-12 05:21:43
【问题描述】:

我正在尝试从附加表中获取 id 并在算术中使用它。

这是我在 php 中附加表格的模式

<tbody>

    <tr class="item-row"  id="item-row" onload="calculate()">

        <?php

        foreach ($conn->query("SELECT * FROM panapricelist") as $info){

            echo "<td><input type='checkbox' id='promotitle' name='check' value='".$info['ProductId']."' ></td>";
            echo "<td><textarea rows='4' cols='7' maxlength='60'  name='pcode' class='pcode' id='ProductCode' disabled>".$info['ProductCode']."</textarea></td>";
            echo "<td><br><textarea rows='5' cols='40' maxlength='50' name='puse' id='productUse' disabled>".$info['ProductUse']." </textarea></td>";
            echo "<td><br><textarea rows='4' cols='50' maxlength='50' name='pdesc' id='productDesc' disabled>".$info['ProductDesc']."</textarea></td>";
            echo "<td id='msrp'><textarea rows='4' cols='10' maxlength='50' name='Msrp' class='productMsrp' id='productMsrp' disabled>".$info['Msrp']."</textarea></td>";
                echo "<td style='width: 10%;' id='cost'><textarea rows='4' cols='10' name='Dealerphp' maxlength='50' class='cost' id='cost' disabled>".$info['DealerPhp']."</textarea></td</td></tr>";

            }
            ?>

</tbody>

这里是附加的 javascript。

<script type="text/javascript">

    $(document).ready(function() {
        $("#button_add").click(function() {
            var favorite = [];
            $.each($("input[name='check']:checked").each( function() {
                // favorite.push($(this).val());

                var getRow = $(this).parents('tr'); //variable for the entire row
                var value =  (getRow.find('td:eq(1)').html()); // Product Code
                var value1 = (getRow.find('td:eq(2)').html()); // for Suggested Product Use
                var value2 = (getRow.find('td:eq(3)').html()); // for product Description
                var value3 = (getRow.find('td:eq(4)').html()); // for MSRP PHP
                var value4 = (getRow.find('td:eq(5)').html()); // for Dealer PHP

                $('#item-row').append('<tr><td class="item-name"><textarea class="productid" id="prc" value="'+ value +'</textarea></td><td class="item-name"><textarea class="productuse" id="productuse" value= "' + value1 + ' </textarea> </td><td class="item-name"><textarea class="description" id="description" value= "' + value2 +' </textarea></td><td class="item-name"><textarea class="msrp" id="msrp" value= "' + value3 + ' </textarea>  </td><td class="item-name"><textarea class="cost" name="cost" id="'+ value4 + ' </textarea></td><td class="item-name"><textarea class="qty" id="qty" name="qty"></textarea></td><td class="item-name"><textarea id="price" class="price" name="price" disabled></textarea></td></tr>');    

                console.log(value4);

            }));
        });
    });

</script>

这是获取 id 的算术 javascript,乘以 qty 列并得到总数。

function update_price() {
  var row = $(this).parents('.item-row');
  var price = row.find('.cost').val().replace("₱" ,"") * row.find('.qty').val();
  price = roundNumber(price,2);
  isNaN(price) ? row.find('.price').html("N/A") : row.find('.price').html("₱" +price);

  update_total();
  update_balance();
  update_ftotal();

}

function bind() {
  $(".cost").blur(update_price);
  $(".qty").blur(update_price);
}

【问题讨论】:

  • 您正在循环中创建多个具有相同 ID 的元素。这是不允许的。在 HTML 中,文档中每个元素的 id 必须是唯一的。
  • 我已经做到了,它没有任何改变。我如何使用 append 中的类或 id 并在算术 javascript 中使用它?

标签: javascript php append arithmetic-expressions


【解决方案1】:

知道了

function update_price() {
                            var row = $(this).parents('tr');
                            var a , w = 0;
                            var a = row.find('.cost').val().replace(/\,/g,''); 
                            var w = row.find('.qty').val().replace(/\,/g,'');

                            var price = Number(a) * Number(w);

                            price = roundNumber(price,2);
                            isNaN(price) ? row.find('.price').html("N/A") : row.find('.price').html("₱" +price);

                            update_total();
                            update_balance();
                            update_ftotal();

                        }

                        function bind() {
                            $(".cost").blur(update_price);
                            $(".qty").blur(update_price);
                        }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-10-26
    • 1970-01-01
    • 1970-01-01
    • 2018-08-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-12
    相关资源
    最近更新 更多