繁体   English   中英

jQuery 中文本框值的总和

[英]Sum of textbox values in jQuery

我正在使用 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();自动添加val(); 所以我不知道如何得到总和。

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

<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/

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM