javascript - 使用javascript自动计算具有来自mysql的数据的表行

标签 javascript php html mysql

大家好,我这里有一个代码,它从 mysql 收集数据并将其显示在 html 表中,这是 php 代码:

<?Php
include("database_connection.php");
$query="select money
        from `money_denomination_t`
        ";
        $select_data_Result = mysqli_query($objconn, $query);
?>

这是 HTMl 代码:

<table id="">
<thead>
<th>Money</th><th>x</th><th>Amount</th><th>=</th><th>Total</th>
<thead>
<?php 
$id = 0;
while($row=mysqli_fetch_array($select_data_Result))
{
    $id++;
?>
<tbody>
<tr>
<td>
<input type="text" name="money[]" value="<?php echo $row['money']; ?>" ></td>
<td>x</td>
<td><input type='text' name="amount[]"></td>
<td>=</td>
<td><input type='text' name="rowtotal[]"></td>
</tr>
</tbody>
<?php
}   
?>
<td></td><td></td><td></td><td></td><td><input type='text' name="total"></td>
</table>

我想做的是获取金钱和金额的乘积,并使用 keyup javascript 将其显示在 rowtotal 中,任何可以帮助我实现它的人

我这里已经有了总结 rowtotal 的所有值并将其显示在文本框总计中的代码,它已经很好了我只需要帮助计算每一行以在此处集成:

<script>
var totalperrow = document.getElementsByName("rowtotal[]");
var totalperrow_array = Array.prototype.slice.call(totalperrow);


for(var i=0; i < totalperrow_array.length; i++){
    totalperrow_array[i].addEventListener("keyup", sum_values);
}
function sum_values(){
    var sum = 0;
    for(var i=0; i < totalperrow_array.length; i++){
        sum += parseFloat(totalperrow_array[i].value);
    }
    if(!isNaN(sum))
    {
        document.getElementsByName("total")[0].value = sum;
        alert(sum);
    }
}
</script>

最佳答案

希望我正确地解释了这个问题 - 下面的代码使用 for 循环来模拟记录集迭代并为每一行生成随机数额的钱。 javascript 查找所有 amount 字段并监听这些字段上的 keyup 事件。 keyup 事件触发该行总计的计算,这也会更新表格底部的总计。顺便说一句,您的原始 HTML 无效 - 最后几个 td 元素必须在表格行内!

<!DOCTYPE html>
<html lang='en'>
    <head>
        <meta charset='utf-8' />
        <title>keyup sum</title>
        <style>
            body,body *{box-sizing:border-box;font-family:calibri,verdana,arial;}
            input[type='text']{padding:0.5rem;}
        </style>
    </head>
    <body>

        <table id="">
            <thead>
                <th>Money</th>
                <th>x</th>
                <th>Amount</th>
                <th>=</th>
                <th>Total</th>
            <thead>
        <?php

        $id = 0;
        for( $i=0; $i < 5; $i++ ){

            $id++;

            $row['money']=mt_rand(5,250);

        ?>
            <tbody>
                <tr>
                    <td>
                        <input type="text" name="money[]" value="<?php echo $row['money']; ?>" >
                    </td>
                    <td>x</td>
                    <td>
                        <input type='text' name="amount[]">
                    </td>
                    <td>=</td>
                    <td>
                        <input type='text' name="rowtotal[]">
                    </td>
                </tr>
        <?php
        }   
        ?>
                <tr>
                    <td colspan=4 align='right'>Grand Total</td>
                    <td><input type='text' name="total"></td>
                </tr>
            </tbody>
        </table>


        <script>

            let total=document.querySelector('input[type="text"][name="total"]');



            const calculatetotal=function(){
                let sum=0;
                Array.prototype.slice.call( document.querySelectorAll( 'input[type="text"][name="rowtotal[]"]' ) ).forEach( field=>{
                    if( !isNaN( parseFloat( field.value ) ) )sum += parseFloat( field.value );
                });
                return sum;
            }



            Array.prototype.slice.call( document.querySelectorAll( 'input[type="text"][name="amount[]"]' ) ).forEach( input=>{
                input.addEventListener('keyup', function(e){
                    if( !isNaN( this.value ) ){
                        let money = this.parentNode.parentNode.querySelector('input[type="text"][name="money[]"]')
                        let result = this.parentNode.parentNode.querySelector('input[type="text"][name="rowtotal[]"]')
                        let product = this.value * money.value;

                        result.value=product;
                        total.value=calculatetotal();
                    }
                })
            })
        </script>


    </body>
</html>

要计算行总数,上述代码在搜索该行中的其他文本字段时引用事件输入的父(表格)行作为基础。使用 querySelector,您可以非常具体地搜索您要搜索的内容 - 非常有用的引用 can be found here

关于javascript - 使用javascript自动计算具有来自mysql的数据的表行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57247971/

相关文章:

javascript - 使用 google plus 登录后从我在网站上的帐户注销

javascript - 有没有比在 app.js 上绑定(bind)模块名称更好的方法来动态加载 View 模型和 View 以显示模式?

javascript - 从 JavaScript 更新属性并删除数组中的重复元素

php - 如何加载 excel 模板并在 PHPExcel 中写入?

php - 实现 fancybox onclick()

html - 如何分离单选按钮?

javascript - webpackConfig 上的 ESM 导入

javascript - 如何导入时间戳类型?

PHP:即使文件存在且可写,unlink 也无法删除文件

javascript - Bootstrap 导航栏在 Chrome 移动版上无法正常工作