简体   繁体   English

获取具有相同父div的所有文本框的值并显示sum

[英]Get the values of all text boxes having same parent div and show sum

I have two div of same class name and each div contains 5 input fields, 4 of them input fields and fifth one is for showing the sum. 我有两个具有相同类名的div,每个div包含5个输入字段,其中4个输入字段,第五个用于显示总和。 Can someone help me in writing a JavaScript which triggers the onblur(), but get the amounts from the input fields of that div only and show the sum in its Total. 有人可以帮助我编写触发onb​​lur()的JavaScript,但只能从该div的输入字段中获取金额,并在总计中显示总和。 The Problem is all the input fields have same class name and even both div have same names so how will i differentiate between them. 问题是所有输入字段都具有相同的类名称,甚至两个div都具有相同的名称,因此我将如何区分它们。 I cant give them ids because i am generating it from a generic loop, the loop gets the data from the database and executes itself for the number of results from database. 我不能给他们id,因为我是从一个通用循环生成它的,该循环从数据库中获取数据并针对数据库中的结果数量执行自身。

<div class="feerow">
    <div class="feetext">
        <input class="amount"  type="text" data-feetype="8" data-clsec="345" />
    </div>
    <div class="feetext">
        <input class="amount"  type="text" data-feetype="9" data-clsec="345" />
    </div>
    <div class="feetext">
        <input class="amount"  type="text" data-feetype="10" data-clsec="345" />
    </div>
    <div class="feetext">
        <input class="amount"  type="text" data-feetype="11" data-clsec="345" />
    </div>
    <div class="feetext">
        <input class="amount" type="text" id="total" readonly="" disabled="disabled" />
    </div>
</div>
<div class="feerow">
    <div class="feetext">
        <input class="amount"  type="text" data-feetype="8" data-clsec="346" />
    </div>
    <div class="feetext">
        <input class="amount"  type="text" data-feetype="9" data-clsec="346" />
    </div>
    <div class="feetext">
        <input class="amount"  type="text" data-feetype="10" data-clsec="346" />
    </div>
    <div class="feetext">
        <input class="amount"  type="text" data-feetype="11" data-clsec="346" />
    </div>
    <div class="feetext">
        <input class="amount" type="text" id="total" readonly="" disabled="disabled" />
    </div>
</div>

Here's an example using plain javascript. 这是使用纯JavaScript的示例。 I couldn't be bothered to test it in multiple browsers, but it seems to work fine in Chrome. 我不愿意在多个浏览器中对其进行测试,但是它似乎可以在Chrome中正常运行。 Basically, whenever someone types something (you can use the input event instead of the blur event for more immediate results) it adds up all the numbers in the row and displays them in the total. 基本上,只要有人键入内容(您可以使用input事件而不是blur事件来获得更直接的结果),它就会将行中的所有数字相加并显示在总数中。 I added a bit of code to format the result as dollars since it looked like you're dealing with money. 我添加了一些代码以将结果格式设置为美元,因为它看起来就像您在处理金钱。

 function sumRow(row) { var fees = row.querySelectorAll("input:not(:disabled)"), total = Array.prototype.reduce.call(fees, function(sum, input) { return sum + (parseFloat(input.value) || 0); }, 0), formatted; if (total) { formatted = "$" + Math.round(100 * total); formatted = formatted.substr(0, formatted.length - 2) + "." + formatted.substr(-2); } else { formatted = "$0.00"; } row.querySelector("input:disabled").value = formatted; } document.addEventListener("input", function(e) { sumRow(e.target.parentElement.parentElement); }); window.addEventListener("load", function onLoad() { window.removeEventListener(onLoad); Array.prototype.forEach.call(document.querySelectorAll(".feerow"), sumRow); }); 
 <div class="feerow"> <div class="feetext"> <input class="amount" type="text" data-feetype="8" data-clsec="345" /> </div> <div class="feetext"> <input class="amount" type="text" data-feetype="9" data-clsec="345" /> </div> <div class="feetext"> <input class="amount" type="text" data-feetype="10" data-clsec="345" /> </div> <div class="feetext"> <input class="amount" type="text" data-feetype="11" data-clsec="345" /> </div> <div class="feetext"> <input class="amount" type="text" id="total" readonly="" disabled="disabled" /> </div> </div> <div class="feerow"> <div class="feetext"> <input class="amount" type="text" data-feetype="8" data-clsec="346" /> </div> <div class="feetext"> <input class="amount" type="text" data-feetype="9" data-clsec="346" /> </div> <div class="feetext"> <input class="amount" type="text" data-feetype="10" data-clsec="346" /> </div> <div class="feetext"> <input class="amount" type="text" data-feetype="11" data-clsec="346" /> </div> <div class="feetext"> <input class="amount" type="text" id="total" readonly="" disabled="disabled" /> </div> </div> 

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

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