简体   繁体   English

JavaScript - 如何在所有 html 行中运行 javascript

[英]JavaScript - How can I run the javascript in all my html rows

I am creating invoice form, I have written a Java Script to calculate Tax and Discount price as the user the value, But it is just working in one row how can I enable it to work on all the rows我正在创建发票表单,我编写了一个 Java 脚本来计算税收和折扣价格作为用户的价值,但它只是在一行中工作,我如何使其能够在所有行上工作

Javascript to calculate discount and tax用于计算折扣和税收的 Javascript

在此处输入图片说明

Html form html格式

在此处输入图片说明

The better option is to do this calculation in PHP itself but if you want to do this in Javascript the you can do something like this:更好的选择是在 PHP 本身中进行此计算,但如果您想在 Javascript 中进行此计算,则可以执行以下操作:

Try creating a JS function that takes in all the values and the id of output input field and make sure that the id of output field is distinct.尝试创建一个 JS 函数,它接受所有值和输出输入字段的 id,并确保输出字段的 id 是不同的。

JS Code JS代码

function calculate(unit_price, quantity, discount, tax, output_id) {
    var subtotal = "do subtotal calc here";
    document.getElementById(output_id).value = subtotal;
}

PHP/HTML Code PHP/HTML 代码

<?php
    // mySQL Code goes here
    while($row = mysql_fetch_array($result, MYSAL_ASSOC))
?>
<div class="centered">
    <table border="1">
    <tr>
        <form id = "invoiceform" action="final.php" method="post">
        <td><input type="text" id="Sl_no" value="<?php echo $s; ?>"></td> <!-- DO NOT INCREASE $S HERE YET-->
        <td><input type="text" id="partno" value="<?php echo $p[$j]; ?>"></td>
        <td><input type="text" id="description" value="<?php $row['Description']; ?>"></td>
        <td><input type="text" id="unit_price" value="<?php echo $row['Unit_Price']; ?>"></td>
        <td><input type="text" id="quantity" value=""></td>
        <td><input type="text" id="discount" value=""></td>
        <td><input type="text" id="tax" value=""></td>
        <td><input type="text" id="<?php echo 'subtotal_'.$s;?>" value=""></td>
        <script>
        calculate(<?php echo $row['Unit_Price']; ?>, <?php echo $row['Quantity']; ?>, <?php echo $row['Discount']; ?>, <?php echo $row['Tax']; ?>, <?php echo 'subtotal_'.$s; s++;?>);
        </script>
    </tr>
    </table>
</div>

PS Try making all the field ids distinct the way i made subtotal id distinct because a HTML page is supposed to have distinct IDs on elements PS尝试使所有字段 id 不同,就像我使小计 id 不同的方式,因为 HTML 页面应该在元素上具有不同的 ID

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

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