表单字段值在jQuery中求和

时间:2014-02-20 13:47:11

标签: javascript jquery

我似乎无法让这个为我正在做的项目工作。基本上我正在尝试将“收入”字段中的值设置为“总收入”字段底部的总数。

我做了一个JSFiddle,希望能让它更容易理解 -

HTML标记:

<div class="form-group">
            <label class="control-label col-md-2">April</label>
            <div class="col-md-3">
              <div class="input-group">
                <span class="input-group-addon">$</span><input class="form-control" name="AprilInput" placeholder="eg. 35,328" type="text" id="AprilInput"></input>
              </div>
            </div>

            <label class="control-label col-md-1">Revenue</label>
            <div class="col-md-3">
              <div class="input-group">
                <span class="input-group-addon">$</span><input class="form-control" name="Output" id="AprilOutput" placeholder="0" type="text" readonly></input>
              </div>
            </div>
      </div>

      <div class="form-group">
            <label class="control-label col-md-2">May</label>
            <div class="col-md-3">
              <div class="input-group">
                <span class="input-group-addon">$</span><input class="form-control" name="MayInput" placeholder="eg. 35,328" type="text" id="MayInput"></input>
              </div>
            </div>

            <label class="control-label col-md-1">Revenue</label>
            <div class="col-md-3">
              <div class="input-group">
                <span class="input-group-addon">$</span><input class="form-control1" name="Output" id="MayOutput" placeholder="0" type="text" readonly></input>
              </div>
            </div>
      </div>

      <div class="form-group">
            <label class="control-label col-md-2">June</label>
            <div class="col-md-3">
              <div class="input-group">
                <span class="input-group-addon">$</span><input class="form-control" name="JuneInput" placeholder="eg. 35,328" type="text" id="JuneInput"></input>
              </div>
            </div>

            <label class="control-label col-md-1">Revenue</label>
            <div class="col-md-3">
              <div class="input-group">
                <span class="input-group-addon">$</span><input class="form-control1" name="Output" id="JuneOutput" placeholder="0" type="text" readonly></input>
              </div>
            </div>
      </div>

<br/>

<span class="form-horizontal">
<div class="row">
  <div class="col-lg-12">
    <div class="widget-container fluid-height clearfix">
      <div class="heading">
        <i class="icon-reorder"></i>Annual Total
      </div>
      <div class="widget-content padded">

      <div class="form-group">
            <label class="control-label col-md-6">Total Revenue</label>
            <div class="col-md-3">
              <div class="input-group">
                <span class="input-group-addon">$</span><input class="form-control" name="TotalOutput" id="TotalOutput" placeholder="0" type="text" readonly></input>
              </div>
            </div>
      </div>

5 个答案:

答案 0 :(得分:1)

您正在循环浏览Output标记。将其更改为.form-contol

$(".form-control").each(function() { /* ... */ }

而不是.html,但.val():

`$("#TotalOutput").val(sum.toFixed(0));`

答案 1 :(得分:1)

你可以稍微整理一下代码:

   function SetupInput(obj,output,sumfunction){
        $(obj).keyup(function(){
            var n = parseInt($(this).val());
            var n = this.value.replace(/,/g, "");

            if(n <= 155000) {
                $(output).val(numberWithCommas((n/100*70).toFixed(0)));
            }
            else if(n <= 175000) {
                $(output).val(numberWithCommas((n/100*75).toFixed(0)));
            }
            else {
                $(output).val(numberWithCommas((n/100*80).toFixed(0)));
            }
            sumfunction();
        });
    }
    SetupInput($('#AprilInput')[0],$('#AprilOutput')[0],calculateSum);
    SetupInput($('#MayInput')[0],$('#MayOutput')[0],calculateSum);
    SetupInput($('#JuneInput')[0],$('#JuneOutput')[0],calculateSum);
    function numberWithCommas(x) {
        return x.toString().replace(/\B(?=(?:\d{3})+(?!\d))/g, ",");
    }



function calculateSum() {
    var sum = 0;
    //iterate through each textboxes and add the values
    $(".form-control1").each(function() {
        //add only if the value is number
        var value=this.value.replace(',','');//remove ','

        if(!isNaN(value) && value.length!=0) {
            sum += parseFloat(value);
            console.log(this.id,sum);
        }
    });

    //.toFixed() method to roundoff the final sum
    $("#TotalOutput").val(sum.toFixed(0));
}

查看jsfiddle:http://jsfiddle.net/2jY6P/43/

答案 2 :(得分:0)

我编辑了代码:http://jsfiddle.net/2jY6P/38/

改变:

$(document).ready(function(){

        //iterate through each textboxes and add keyup
        //handler to trigger sum event


            $("input[name='Output']").keyup(function(){

                calculateSum();
            });

    });

    function calculateSum() {

        var sum = 0;
        //iterate through each textboxes and add the values
        $("input[name='Output']").each(function() {

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

        });
        //.toFixed() method to roundoff the final sum
        $("#TotalOutput").val(sum.toFixed(0));
    }

$('Output')应输入$("[name='Output']")

  

$( “#TotalOutput”)HTML(sum.toFixed(0));

应为$("#TotalOutput").val(sum.toFixed(0));

答案 3 :(得分:0)

我在

中进行了一些更改

http://jsfiddle.net/2jY6P/39/

$(document).keyup(function() {
    var sumRevenue = 0;
    $.each($(".revenue"), function() {
       var val = $.trim($(this).val());
       if(val != "")
           sumRevenue += parseFloat(val); 
    });
    $("#sumrevenue").val(sumRevenue);
});

答案 4 :(得分:0)

   function calculateTotalREv(){ 
      var totalRev = 0;
  $("input[name='Output']").each(function() {
    totalRev = eval(total+parseFloat($(this).val()));
  });
      alert(totalRev);
    }
    calculateTotalREv();