简体   繁体   English

使用脚本添加两个相同名称的数组值

[英]Add two array values of same name using script

I need to multiply two values of a and b and display in c. 我需要将a和b的两个值相乘并在c中显示。 Also I need to display the addition value of c using script. 我还需要使用脚本显示c的附加值。 Please check my code below: 请在下面检查我的代码:

HTML: HTML:

        <table border="1" id="table" class="table">
            <tr>
                <th>a</th>
                <th>b</th>
                <th>c</th>
            </tr>

                <?php
                $select= "SELECT * from table where id=1";
                            $result = mysql_query($select);
                            $row_count=1;
                            while($row = mysql_fetch_assoc($result))
        {
                ?>  
<tr>    
            <td><input type="text" value="<?php echo $row['name_a']; ?>" name="name_a[]" class="name_a"></td>
            <td><input type="text" value="<?php echo $row['code_a']; ?>" name="code_a[]" class="code_a"></td>
            <td><input type="text" name="id_a[]" class="id_a" ></td>
</tr>
        <?php
        $row_count++;
        }
        ?>

</table>
    <input type="text" name="total" class="total">

SCRIPT: 脚本:

$(document).ready(function() {
    $('#table').on('keyup', '.name_a', cal)
        .on('keyup', '.code_a', cal)
        .on('keyup', '.id_a', cal);

    function cal() {
        var $row = $(this).closest('tr'),
        name_a = $row.find('.name_a').val(),
        code_a = $row.find('.code_a').val(),
        id_a = $row.find('.id_a').val(),
        id_a_calc = name_a * code_a;

        $row.find('.id_a').val(id_a_calc)

    }
});

OUTPUT 输出值

a   b   c 
1   2   2
2   4   8

By the above script,I can multiply a and b and display in c.But how to add two values of 'c' and display in total text box.Need help 通过上面的脚本,我可以将a和b相乘并在c中显示,但是如何将'c'的两个值相加并在总文本框中显示。

try this: 尝试这个:

 $(document).ready(function() {
        $('#table').on('keyup', '.name_a', cal)
            .on('keyup', '.code_a', cal)
            .on('keyup', '.id_a', cal); 
    });

    function cal() {
        var $row = $(this).closest('tr'),
        name_a = $row.find('.name_a').val(),
        code_a = $row.find('.code_a').val(),
        id_a = $row.find('.id_a').val(),
        id_a_calc = name_a * code_a;

        $row.find('.id_a').val(id_a_calc);
        var total = 0;
        $(".id_a").each(function() {
           // console.log($(this).val());
            if($(this).val().length > 0){
            total += parseInt($(this).val(), 10);
            }
        });
        $(".total").val(total);

    }

Put this in cal after you update $row.find('.id_a').val() 在更新$row.find('.id_a').val()之后,将其放入cal

var total = 0;
$(".id_a").each(function() {
    total += parseInt($(this).val(), 10);
});
$(".total").text(total);

try this code:- 试试这个代码:

$(document).ready(function() {
    $("#btn").click(function() {
        var cValue = 0;
        $('#table tr td:nth-child(3)').each(function() { 
            cValue += parseInt($(this).html());
        });
        $("#total").val(cValue)
    }); 
});

jsfiddle example:- http://jsfiddle.net/c2S5d/21/ jsfiddle示例:-http: //jsfiddle.net/c2S5d/21/

暂无
暂无

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

相关问题 如何使用JavaScript将两个值添加到数组中 - How to add two values into the array using JavaScript 如何将两个值添加到地图类型数组中的同一索引 - How to add two values to the same index in map type array 使用javascript查找同一对象数组中存在的两个值 - Finding two values existing in same object array using javascript MongDB 聚合两个同名值 - MongDB Aggregate two values with same name 如何将存储在数组中的值作为 textContent 或 innerText 添加到具有相同 class 名称的 div 元素 - How to add values, that are stored in an array, as textContent or innerText to div elements with the same class name 如何将具有两对对象的对象添加到具有与对象相同的键和值的数组 - how to add an object with two pairs to an array that has a key and values that are same as the object 比较 object 的两个 arrays 具有相同的值,并在 Vue.js2 中将一个数组的属性转换/添加到另一个数组 - Compare two arrays of object with same values and chenge/add a property of one array to the other one in Vue.js2 显示数组值(如果数组名和字符串名相同) - Display array values(if array name and string name is same) jQuery - 将数组值设置为具有相同名称的输入 - jQuery - set array values to the input with the same name 如何使用jQuery在两个不同的地方检查和取消选中具有相同名称的复选框的值? - How to checked and unchecked values of checkboxes with same name two different places using jquery?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM