jquery中文本框值的总和

时间:2016-12-08 09:27:08

标签: javascript jquery ajax javascript-events codeigniter-2

我使用ajax,我需要文本框值的总和,并自动显示该值到另一个文本框我的文本框ID是selprice真的我需要selprice的总和,我的ajax在这里,如果有任何选项要做结果请帮帮我......

$.ajax({
  url: "<?php echo site_url('moderator/Cashinvoice/check_proid/') ?>/" + id,
  type: "POST",
  dataType: "JSON",
  success: function(data) {
    //                alert(quantity);
    var stockcount = data.mro_stockcount;

    if (data == 0) {
      $('#main').html('<span id="li" style="color:red">Incorrect Product ID.</spam>');
    } else if (stockcount < parseInt(quantity)) {
      $('#main').html('<span id="li" style="color:red"> You are entered quantity is not in stock and total stock count is : <input type="text" id="stok" name="qua" readonly value="" style="width:40px; color:black; background-color: #eeeeee; test-align: center; padding-left: 4px;" ></spam>');
      $('[id="stok"]').val(data.mro_stockcount);

    } else {
      iCnt = iCnt + 1;
      if (iCnt == 1) {
        $(container).append('<table id="example-rfi" class="table table-bordered table-striped"><tr>\n\
                   <th>Product Name</th><th>Selling Price</th><th>Discount</th>\n\
                    <th>Total price</th><th>VAT</th><th>CST</th><th>Excise</th>\n\
                    <th>Other Tax</th></tr>');
      }
      $(container).append('<table id="example-rfi" class="table"><tr><td><input type=text readonly class="input" style="width: 200px;" id=tb' + iCnt + ' ' +
        'value="" /></td><td><input type=text readonly class="input" style="width: 70px;" id=selprice' + iCnt + ' ' +
        ' \n\
                                    value="" /></td><td><input type=text readonly class="input" style="width: 50px;" id=discount' + iCnt + ' ' +
        ' \n\
                                    value="" /></td><td><input type=text readonly class="input" name="total" style="width: 70px;" id=selprice' + iCnt + ' ' +
        ' \n\
                                    value="" /></td><td><input type=text readonly class="input" style="width: 50px;" id=vat' + iCnt + ' ' +
        ' \n\
                                    value="" /></td><td><input type=text readonly class="input" style="width: 50px;" id=cst' + iCnt + ' ' +
        ' \n\
                                    value="" /></td><td><input type=text readonly class="input" style="width: 50px;" id=excise' + iCnt + ' ' +
        ' \n\
                                    value="" /></td><td><input type=text readonly class="input" style="width: 50px;" id=othrtax' + iCnt + ' ' +
        ' \n\
                                    value="" /></td></tr></table>');
      //                '<tr><td><input type=text readonly class="input" style="width: 200px;" id=tb' + iCnt + ' ' +
      //                            'value="" /></td><td><input type=text readonly class="input" style="width: 100px;" id=selingprice' + iCnt + ' ' +
      //                            ' \n\
      //                            value="" /></td></tr></table>');
      if (iCnt == 1) {
        var divSubmit = $(document.createElement('div'));
        $(divSubmit).append('<input type=button  class="btn btn-primary"' +
          'onclick="GetTextValue()"' +
          'id=btSubmit value=GO />');
        // ADD BOTH THE DIV ELEMENTS TO THE "main" CONTAINER.
        $('#mainn').after(container, divSubmit);


      }

      $('[id="quant' + iCnt + '"]').val(quantity);
      $('[id="tb' + iCnt + '"]').val(data.mro_name);
      $('[id="vat' + iCnt + '"]').val(data.mro_vat);
      $('[id="cst' + iCnt + '"]').val(data.mro_cst);
      $('[id="octr' + iCnt + '"]').val(data.mro_octroi);
      $('[id="tax' + iCnt + '"]').val(data.mro_servicetax);
      $('[id="excise' + iCnt + '"]').val(data.mro_excise);
      $('[id="othrtax' + iCnt + '"]').val(data.mro_othertax);
      $('[id="discount' + iCnt + '"]').val(data.mro_discount);
      $('[id="quantity' + iCnt + '"]').val(quantity);
      $('[id="selingprice' + iCnt + '"]').val(data.mro_sellingprice);
      $('[id="selprice' + iCnt + '"]').val(data.mro_sellingprice * quantity);
      $('[id="proid' + iCnt + '"]').val(data.mro_id);

      $('#dis').append('<input type=hidden class="input" name="productid[]" id=proidd' + iCnt + ' ' +
        '  value="" />\n\<input type=hidden class="input" name="quantityy[]" id=quantt' + iCnt + ' ' +
        '  value="" />');
      $('[id="proidd' + iCnt + '"]').val(data.mro_id);
      $('[id="quantt' + iCnt + '"]').val(quantity);
    }
  },
  //                alert(quant);
  error: function(jqXHR, textStatus, errorThrown) {
    $('#main').html('<span id="li" style="color:red">Please provide Product ID.</spam>');
  }
});

文本框值由val()自动添加;所以idont知道怎么得到总和

1 个答案:

答案 0 :(得分:0)

这是两个输入框的和与另一个文本输入框的总和的代码。

<input type="text" id="txt1"  onkeyup="sum();" />
<input type="text" id="txt2"  onkeyup="sum();" />
<input type="text" id="txt3" />

<script>
function sum() {
        var txtFirstNumberValue = document.getElementById('txt1').value;
        var txtSecondNumberValue = document.getElementById('txt2').value;
        var result = parseInt(txtFirstNumberValue) + parseInt(txtSecondNumberValue);
        if (!isNaN(result)) {
            document.getElementById('txt3').value = result;
        }
    }
</script>

这是链接:http://jsfiddle.net/tLKLy/