2012-11-24 92 views
11

我并不完美的Javascript ..我想显示在下一个输入框中输入的数值输入框中的总和,而不刷新页面。任何人都可以帮我弄清楚..?如何使用Javascript从输入框值中获取总和?

这里是JavaScript的

<script type="text/javascript"> 
var howmanytoadd = 2; 
var rows; 

function calc() { 
    var tot = 0; 
    for (var i = 0; i < rows.length; i++) { 
     var linetot = 0; 
     rows[i].getElementsByTagName('input')[howmanytoadd].value = linetot; 
     tot += linetot; 
    } 
    document.getElementById('total').value = tot 
} 
onload = function() { 
    rows = document.getElementById('tab').getElementById('qty1'); 
    for (var i = 0; i < rows.length; i++) { 
     rows.getElementsByTagName('input')[i].onkeyup = calc; 
    } 
} 
</script> 

这里是我的html代码:

Qty1 : <input type="text" name="qty1" id="qty"/><br> 
Qty2 : <input type="text" name="qty2" id="qty"/><br> 
Qty3 : <input type="text" name="qty3" id="qty"/><br> 
Qty4 : <input type="text" name="qty4" id="qty"/><br> 
Qty5 : <input type="text" name="qty5" id="qty"/><br> 
Qty6 : <input type="text" name="qty6" id="qty"/><br> 
Qty7 : <input type="text" name="qty7" id="qty"/><br> 
Qty8 : <input type="text" name="qty8" id="qty"/><br> 
<br><br> 
Total : <input type="text" name="total" id="total"/> 

这里是截屏 here is screen shot

+3

@SwapnilBhavsar把你的代码中的问题,而不是一个注释。 – xdazz

+4

所以你知道,'HTML'中的'id'属性必须是唯一的,这意味着你不能在每个页面使用多个元素。 – Maroshii

+1

ID必须是唯一的。为每个文本框使用不同的ID –

回答

25

尝试:

Qty1 : <input onblur="findTotal()" type="text" name="qty" id="qty1"/><br> 
Qty2 : <input onblur="findTotal()" type="text" name="qty" id="qty2"/><br> 
Qty3 : <input onblur="findTotal()" type="text" name="qty" id="qty3"/><br> 
Qty4 : <input onblur="findTotal()" type="text" name="qty" id="qty4"/><br> 
Qty5 : <input onblur="findTotal()" type="text" name="qty" id="qty5"/><br> 
Qty6 : <input onblur="findTotal()" type="text" name="qty" id="qty6"/><br> 
Qty7 : <input onblur="findTotal()" type="text" name="qty" id="qty7"/><br> 
Qty8 : <input onblur="findTotal()" type="text" name="qty" id="qty8"/><br> 
<br><br> 
Total : <input type="text" name="total" id="total"/> 


    <script type="text/javascript"> 
function findTotal(){ 
    var arr = document.getElementsByName('qty'); 
    var tot=0; 
    for(var i=0;i<arr.length;i++){ 
     if(parseInt(arr[i].value)) 
      tot += parseInt(arr[i].value); 
    } 
    document.getElementById('total').value = tot; 
} 

    </script> 
+0

感谢m8它的工作... –

+0

;)..................... –

1

试试这个:

function add() 
{ 
    var sum = 0; 
    var inputs = document.getElementsByTagName("input"); 
    for(i = 0; i <= inputs.length; i++) 
    { 
    if(inputs[i].name == 'qty'+i) 
    { 
    sum += parseInt(input[i].value); 
    } 
    } 
    console.log(sum) 

} 
6

的Javascript:

window.sumInputs = function() { 
    var inputs = document.getElementsByTagName('input'), 
     result = document.getElementById('total'), 
     sum = 0;    

    for(var i=0; i<inputs.length; i++) { 
     var ip = inputs[i]; 

     if (ip.name && ip.name.indexOf("total") < 0) { 
      sum += parseInt(ip.value) || 0; 
     } 

    } 

    result.value = sum; 
}​ 

HTML:

Qty1 : <input type="text" name="qty1" id="qty"/><br> 
Qty2 : <input type="text" name="qty2" id="qty"/><br> 
Qty3 : <input type="text" name="qty3" id="qty"/><br> 
Qty4 : <input type="text" name="qty4" id="qty"/><br> 
Qty5 : <input type="text" name="qty5" id="qty"/><br> 
Qty6 : <input type="text" name="qty6" id="qty"/><br 
Qty7 : <input type="text" name="qty7" id="qty"/><br> 
Qty8 : <input type="text" name="qty8" id="qty"/><br> 
<br><br> 
Total : <input type="text" name="total" id="total"/> 

<a href="javascript:sumInputs()">Sum</a> 

例子:http://jsfiddle.net/fRd9N/1/

+0

我知道这是一个相当古老的问题,但道具来@Burlak llia。这正是我现在需要的东西! –

0

我需要总结的span元素,所以我编辑阿克hil Sekharan的答案在下面。

var arr = document.querySelectorAll('span[id^="score"]'); 
var total=0; 
    for(var i=0;i<arr.length;i++){ 
     if(parseInt(arr[i].innerHTML)) 
      total+= parseInt(arr[i].innerHTML); 
    } 
console.log(total) 

您可以使用其他元素更改元素链接将引导您进行编辑。

https://www.w3.org/TR/css3-selectors/#attribute-substrings

2

$(document).ready(function(){ 
 

 
\t \t //iterate through each textboxes and add keyup 
 
\t \t //handler to trigger sum event 
 
\t \t $(".txt").each(function() { 
 

 
\t \t \t $(this).keyup(function(){ 
 
\t \t \t \t calculateSum(); 
 
\t \t \t }); 
 
\t \t }); 
 

 
\t }); 
 

 
\t function calculateSum() { 
 

 
\t \t var sum = 0; 
 
\t \t //iterate through each textboxes and add the values 
 
\t \t $(".txt").each(function() { 
 

 
\t \t \t //add only if the value is number 
 
\t \t \t if(!isNaN(this.value) && this.value.length!=0) { 
 
\t \t \t \t sum += parseFloat(this.value); 
 
\t \t \t } 
 

 
\t \t }); 
 
\t \t //.toFixed() method will roundoff the final sum to 2 decimal places 
 
\t \t $("#sum").html(sum.toFixed(2)); 
 
\t }
body { 
 
\t \t \t \t font-family: sans-serif; 
 
\t \t \t } 
 
\t \t \t #summation { 
 
\t \t \t \t font-size: 18px; 
 
\t \t \t \t font-weight: bold; 
 
\t \t \t \t color:#174C68; 
 
\t \t \t } 
 
\t \t \t .txt { 
 
\t \t \t \t background-color: #FEFFB0; 
 
\t \t \t \t font-weight: bold; 
 
\t \t \t \t text-align: right; 
 
\t \t \t }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script> 
 

 
<table width="300px" border="1" style="border-collapse:collapse;background-color:#E8DCFF"> 
 
\t <tr> 
 
\t \t <td width="40px">1</td> 
 
\t \t <td>Butter</td> 
 
\t \t <td><input class="txt" type="text" name="txt"/></td> 
 
\t </tr> 
 
\t <tr> 
 
\t \t <td>2</td> 
 
\t \t <td>Cheese</td> 
 
\t \t <td><input class="txt" type="text" name="txt"/></td> 
 
\t </tr> 
 
\t <tr> 
 
\t \t <td>3</td> 
 
\t \t <td>Eggs</td> 
 
\t \t <td><input class="txt" type="text" name="txt"/></td> 
 
\t </tr> 
 
\t <tr> 
 
\t \t <td>4</td> 
 
\t \t <td>Milk</td> 
 
\t \t <td><input class="txt" type="text" name="txt"/></td> 
 
\t </tr> 
 
\t <tr> 
 
\t \t <td>5</td> 
 
\t \t <td>Bread</td> 
 
\t \t <td><input class="txt" type="text" name="txt"/></td> 
 
\t </tr> 
 
\t <tr> 
 
\t \t <td>6</td> 
 
\t \t <td>Soap</td> 
 
\t \t <td><input class="txt" type="text" name="txt"/></td> 
 
\t </tr> 
 
\t <tr id="summation"> 
 
\t \t <td>&nbsp;</td> 
 
\t \t <td align="right">Sum :</td> 
 
\t \t <td align="center"><span id="sum">0</span></td> 
 
\t </tr> 
 
</table>

相关问题