簡體   English   中英

Javascript - 我看不到值總和的結果

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

我在腳本中有一個設計問題,它給出了右列的值(忽略左邊的值),當我點擊“查看”時,我在綠色框中看不到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> 

我究竟做錯了什么...?

提前致謝!

$(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');

});

你沒有對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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM