简体   繁体   English

使用Javascript自动求和添加数字按钮的输入值

[英]Auto Sum Input Value of Add Number Button Using Javascript

I want to update the Input field with a button and auto display of all Input fields above.我想用一个按钮更新输入字段并自动显示上面的所有输入字段。 If the button is pressed, the input will automatically add a number and if there is an addition, the total input must also be added automatically.如果按下按钮,输入将自动添加一个数字,如果有添加,总输入也必须自动添加。

 function eoscounting() { var Eos = parseInt(document.getElementById('eos_count').value, 10); Eos = isNaN(Eos) ? 0 : Eos; Eos++; document.getElementById('eos_count').value = Eos; } function basocounting() { var Baso = parseInt(document.getElementById('baso_count').value, 10); Baso = isNaN(Baso) ? 0 : Baso; Baso++; document.getElementById('baso_count').value = Baso; } function stabcounting() { var Stab = parseInt(document.getElementById('stab_count').value, 10); Stab = isNaN(Stab) ? 0 : Stab; Stab++; document.getElementById('stab_count').value = Stab; } function segcounting() { var Seg = parseInt(document.getElementById('seg_count').value, 10); Seg = isNaN(Seg) ? 0 : Seg; Seg++; document.getElementById('seg_count').value = Seg; } function monocounting() { var Mono = parseInt(document.getElementById('mono_count').value, 10); Mono = isNaN(Mono) ? 0 : Mono; Mono++; document.getElementById('mono_count').value = Mono; } function limcounting() { var Lim = parseInt(document.getElementById('lim_count').value, 10); Lim = isNaN(Lim) ? 0 : Lim; Lim++; document.getElementById('lim_count').value = Lim; } function findTotal() { const fees = document.querySelectorAll(".fee"); const total = document.querySelector("#total"); let sum = 0; fees.forEach(fee => { if (fee.valueAsNumber) { sum += fee.valueAsNumber; } }); total.value = sum; }
 <input type="number" id="eos_count" class="fee" name="qty" onchange="findTotal()" /> <input type="number" id="baso_count" class="fee" name="qty" onchange="findTotal()" /> <input type="number" id="stab_count" class="fee" name="qty" onchange="findTotal()" /> <input type="number" id="seg_count" class="fee" name="qty" onchange="findTotal()" /> <input type="number" id="mono_count" class="fee" name="qty" onchange="findTotal()" /> <input type="number" id="lim_count" class="fee" name="qty" onchange="findTotal()" /> <br /><br /> <input type="number" name="total" id="total" /> <br /><br /> <button class="input" value="myvalue" onclick="eoscounting()">Eosinofil</button> <button class="input" value="myvalue" onclick="basocounting()">Basofil</button> <button class="input" value="myvalue" onclick="stabcounting()">Netrofil Batang</button> <button class="input" value="myvalue" onclick="segcounting()">Netrofil Segmen</button> <button class="input" value="myvalue" onclick="monocounting()">Monosit</button> <button class="input" value="myvalue" onclick="limcounting()">Limfosit</button>

Any help would be appreciated.任何帮助,将不胜感激。

The on change event isn't being fired by your javascript because of the reason below.由于以下原因,您的 JavaScript 不会触发 on change 事件。

change改变
The change event occurs when a control loses the input focus and its value has been modified since gaining focus.当控件失去输入焦点并且其值在获得焦点后已被修改时,会发生更改事件。 This event is valid for INPUT, SELECT, and TEXTAREA.此事件对 INPUT、SELECT 和 TEXTAREA 有效。 element.元素。

You would need to manually trigger the onChange event, however I'm not completely sure how to do that.您需要手动触发 onChange 事件,但是我不完全确定如何执行此操作。 A quick solution would be to call the findTotal() function inside each of your counting functions.一个快速的解决方案是在每个计数函数中调用findTotal()函数。

 function eoscounting() { var Eos = parseInt(document.getElementById('eos_count').value, 10); Eos = isNaN(Eos) ? 0 : Eos; Eos++; document.getElementById('eos_count').value = Eos; findTotal(); } function basocounting() { var Baso = parseInt(document.getElementById('baso_count').value, 10); Baso = isNaN(Baso) ? 0 : Baso; Baso++; document.getElementById('baso_count').value = Baso; findTotal(); } function stabcounting() { var Stab = parseInt(document.getElementById('stab_count').value, 10); Stab = isNaN(Stab) ? 0 : Stab; Stab++; document.getElementById('stab_count').value = Stab; findTotal(); } function segcounting() { var Seg = parseInt(document.getElementById('seg_count').value, 10); Seg = isNaN(Seg) ? 0 : Seg; Seg++; document.getElementById('seg_count').value = Seg; findTotal(); } function monocounting() { var Mono = parseInt(document.getElementById('mono_count').value, 10); Mono = isNaN(Mono) ? 0 : Mono; Mono++; document.getElementById('mono_count').value = Mono; findTotal(); } function limcounting() { var Lim = parseInt(document.getElementById('lim_count').value, 10); Lim = isNaN(Lim) ? 0 : Lim; Lim++; document.getElementById('lim_count').value = Lim; findTotal(); } function findTotal() { const fees = document.querySelectorAll(".fee"); const total = document.querySelector("#total"); let sum = 0; fees.forEach(fee => { if (fee.valueAsNumber) { sum += fee.valueAsNumber; } }); total.value = sum; }
 <input type="number" id="eos_count" class="fee" name="qty" /> <input type="number" id="baso_count" class="fee" name="qty" /> <input type="number" id="stab_count" class="fee" name="qty" /> <input type="number" id="seg_count" class="fee" name="qty" /> <input type="number" id="mono_count" class="fee" name="qty" /> <input type="number" id="lim_count" class="fee" name="qty" /> <br /><br /> <input type="number" name="total" id="total" /> <br /><br /> <button class="input" value="myvalue" onclick="eoscounting()">Eosinofil</button> <button class="input" value="myvalue" onclick="basocounting()">Basofil</button> <button class="input" value="myvalue" onclick="stabcounting()">Netrofil Batang</button> <button class="input" value="myvalue" onclick="segcounting()">Netrofil Segmen</button> <button class="input" value="myvalue" onclick="monocounting()">Monosit</button> <button class="input" value="myvalue" onclick="limcounting()">Limfosit</button>

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

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