我在做的页面中,有这样一个情况,这个price[X]个数不确定,有时多有时少,现在要将这些input中的值进行累加求和,并显示在<input type="text" name="priceTotal" id="priceTotal">中。<input type="text" name="price1" id="price1">
<input type="text" name="price2" id="price2">
<input type="text" name="price3" id="price3">
<input type="text" name="price4" id="price4">
……
我是这样写的,但不对:
function sum_total(){
var i=1;
i++;
{$("priceTotal").value =$("priceTotal").value+$("price"+i+"").value;}
}各位帮我看看呢谢谢!!!!!!
js累加求和

解决方案 »

  1.   


    <script src="http://code.jquery.com/jquery-latest.js"></script>
    <script type="text/javascript">
    $(function(){//先在下面4个文本框中写值,在点按钮
    $("#abv").click(function(){
    var s=0;
    $("div input").each(function(){
    s=s+parseInt($(this).val());
    });
    $("#priceTotal").val(s);
    });
    })
     </script>
    <input type="text" name="priceTotal" id="priceTotal"/>
     <input type="button" id="abv" value="测试用按钮"/>
    <div id="div1">
    <input type="text" name="price1" id="price1" />
     <input type="text" name="price2" id="price2" />
     <input type="text" name="price3" id="price3" />
     <input type="text" name="price4" id="price4" />
    </div>
      

  2.   


    function sum_total(){
    var sum = 0;
    $(":text[name^='price']").each(function(){
    if($(this).val()){
    sum += parseInt($(this).val());
    }
    });
    $("priceTotal").value(sum);
    }
      

  3.   


    <script type="text/javascript">
       window.onload = function(){
    document.getElementById('abv').onclick = function(){
    var sum = 0;
    for(var i = 1 ; i < 5 ; ++i){
    sum += +document.getElementById('price'+i).value;
    }
    document.getElementById('priceTotal').value = sum;
    }
    }
     </script>
    <input type="text" name="priceTotal" id="priceTotal"/>
     <input type="button" id="abv" value="测试用按钮"/>
    <div id="div1">
    <input type="text" name="price1" id="price1" />
     <input type="text" name="price2" id="price2" />
     <input type="text" name="price3" id="price3" />
     <input type="text" name="price4" id="price4" />
    </div>
      

  4.   

    1、楼主的选择器使用不正确。$("priceTotal")
       priceTotal是id名称,应该使用$("#priceTotal"),后面的类似。
    2、$("#priceTotal").value 的值是string类型的,要进行数学运算,请将其转换为number
    3、这明显的是个语法错误嘛
    function sum_total(){
    var i=1;
    i++;
    {$("priceTotal").value =$("priceTotal").value+$("price"+i+"").value;}
    }
    试下下面这个吧。var total = 0,i=1;
    function sum_total(){
        var ele = document.getElementById("price"+i);
        i++;
        if(ele){
           if(!isNaN(ele.value)){
                return Number(ele.value);
           }
           total += sum_total();
        }
    }
    document.getElementById("priceTotal").value = total;
      

  5.   

    写错了。
    var total = 0,i=1;
    function sum_total(){
        var ele = document.getElementById("price"+i);
        i++;
        if(ele){
           if(!isNaN(ele.value)){
                total += Number(ele.value);
           }
           sum_total();
        }
    }
    sum_total();
    document.getElementById("priceTotal").value = total;
      

  6.   

    unction sum_total(){
    $('#priceTotal').val(0);
    $('[name^=price]').each(function(){
        $('#priceTotal').val(parseFloat($('#priceTotal').val()) + parseFloat($(this).val()));
    });}