简体   繁体   English

Javascript - 我看不到值总和的结果

[英]Javascript - I can not see the result of the sum of values

I have a design problem in the script that gives me the values ​​of the right column (ignoring the values ​​on the left), and the result of the sume I can not see in the green box when I click on "View Result" 我在脚本中有一个设计问题,它给出了右列的值(忽略左边的值),当我点击“查看”时,我在绿色框中看不到sume的结果结果”

 // Old script /*window.sumInputs = function() { var inputs = document.getElementsByTagName('input'), result = document.getElementById('total'), sumar = 0; for(var i=0; i<inputs.length; i++) { var ip = inputs[i]; if (ip.name && ip.name.indexOf("total") < 0) { sumar += parseInt(ip.value) || 0; } } result.value = sumar; }*/ // ======================== // New script $(document).ready(function() { var valores = $('#derecha').children(); var suma = 0; $.each(valores, function() { valor = $(this).val() || 0; suma += parseInt(valor); }); //console.log(suma); valores = document.getElementById('total'); }); 
 body p { margin: 0 20px } /*#izquierda {display:none}*/ #izquierda, #derecha { display: inline-block; vertical-align: top; width: 140px; margin: 20px 20px 20px 20px; padding: 10px; border: 1px solid #000 } #izquierda span, #derecha span, body span { font-weight: bold } #izquierda p, #derecha p { margin: 5px auto 15px; text-align: center } input { width: 80px; display: block; margin: 5px auto; padding: 2px 0; background: #f2f2f2; border: none; border: 1px solid #000; text-align: center } #cont-resultado { text-align: center; width: 120px; padding-left: 40px } #cont-resultado input { display: inline-block; margin: 0 auto 10px; background: red; color: #fff; border: none; padding: 10px 0 } #cont-resultado a { display: inline-block; text-decoration: none; color: #fff; background: green; padding: 10px 12px } #total { display: block } 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div id="izquierda"> <p><span>DIV LEFT</span><br>display="none"</p> <input name="qty1" value="240"> <input name="qty2" value="862"> <input name="qty3" value="911"> <input name="qty4" value=""> <input name="qty5" value=""> <input name="qty6" value=""> <input name="qty7" value=""> <input name="qty8" value=""> </div> <!-- ================ --> <div id="derecha"> <p><span>DIV RIGHT</span><br>display="block"</p> <input name="qty1" value="2"> <input name="qty2" value="2"> <input name="qty3" value="2"> <input name="qty4" value=""> <input name="qty5" value=""> <input name="qty6" value=""> <input name="qty7" value=""> <input name="qty8" value=""> </div> <!-- ================ --> <div id="cont-resultado"> <input name="total" id="total"> <a href="javascript:sumInputs()">See total</a> </div> <br> <p>What I am looking for is that only the RIGHT column is sumed, ignoring the values in the left column. <br><br><span>The result of example (6) must be seen in the red box...</span></p> 

What am I doing wrong...? 我究竟做错了什么...?

Thanks in advance! 提前致谢!

$(document).ready(function(){
var valores = $('#derecha').children();
var suma = 0;
$.each(valores,function(){
  valor = $(this).val() || 0;
  suma += parseInt( valor );
});

//console.log(suma);
valores = document.getElementById('total');

});

You're not doing anything with suma , further, use a selector for the children .children('input') . 你没有对suma做任何事情,进一步,为孩子使用选择器.children('input')

 $(document).ready(function() { function sumInputs(e) { e.preventDefault(); var valores = $('#derecha').children('input'); var suma = 0; $.each(valores, function() { valor = $(this).val(); suma += Number(valor); }); valores = document.getElementById('total'); $(valores).val(suma); } $('#sumup').on('click', sumInputs); }); 
 body p { margin: 0 20px}/*#izquierda {display:none}*/#izquierda,#derecha { display: inline-block; vertical-align: top; width: 140px; margin: 20px 20px 20px 20px; padding: 10px; border: 1px solid #000}#izquierda span,#derecha span,body span { font-weight: bold}#izquierda p,#derecha p { margin: 5px auto 15px; text-align: center}input { width: 80px; display: block; margin: 5px auto; padding: 2px 0; background: #f2f2f2; border: none; border: 1px solid #000; text-align: center}#cont-resultado { text-align: center; width: 120px; padding-left: 40px}#cont-resultado input { display: inline-block; margin: 0 auto 10px; background: red; color: #fff; border: none; padding: 10px 0}#cont-resultado a { display: inline-block; text-decoration: none; color: #fff; background: green; padding: 10px 12px}#total { display: block} 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script><div id="izquierda"> <p><span>DIV LEFT</span><br>display="none"</p> <input name="qty1" value="240"> <input name="qty2" value="862"> <input name="qty3" value="911"> <input name="qty4" value=""> <input name="qty5" value=""> <input name="qty6" value=""> <input name="qty7" value=""> <input name="qty8" value=""></div><!-- ================ --><div id="derecha"> <p><span>DIV RIGHT</span><br>display="block"</p> <input name="qty1" value="2"> <input name="qty2" value="2"> <input name="qty3" value="2"> <input name="qty4" value=""> <input name="qty5" value=""> <input name="qty6" value=""> <input name="qty7" value=""> <input name="qty8" value=""></div><!-- ================ --><div id="cont-resultado"> <input name="total" id="total"> <a id='sumup' href="#">See total</a></div><br><p>What I am looking for is that only the RIGHT column is sumed, ignoring the values in the left column. <br><br><span>The result of example (6) must be seen in the red box...</span></p> 

你需要将valores (新的)的值设置为suma

valores.val(suma);

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

相关问题 我有代码可以使用javascript求和两个文本框值 - i have code it can be sum two textbox values using javascript 如何对 javascript 中的两个时间值求和 - How can I do sum two time values in javascript javascript-如何通过在 Chrome 中调试在一个 if 语句中查看每个条件的结果? - javascript-how can I see result of every condition in one if statement By Debugging in Chrome? 如何正确总结 2 个 JavaScript 承诺的结果并在承诺中返回结果? - How can I correctly sum 2 JavaScript promise's results and return the result in a promise? 如何使用回调 function 实现无限柯里化总和值,结果为 javascript? - How to implement infinite currying sum values with a callback function with result in javascript? 无法对 javascript 中的 for 循环内的值求和 - can't sum values inside a for loop in javascript JavaScript检查以查看总和是否正确 - JavaScript checking to see if a sum is correct 如何查看包含的 JavaScript 源代码? - How can I see included JavaScript sources? Fusionchart已加载,但我看不到-javascript - Fusionchart is loaded but i can't see it - javascript JavaScript验证无效-我看不到任何错误 - Javascript validation not working - no error I can see
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM