在jquery中结合使用Auto Comma和Auto Sum

时间:2017-09-11 06:12:55

标签: javascript jquery keyup

我有这种语法,我希望每当我输入值时,结合使用文本框autocomma和自动结果的脚本。有更好的方法吗?

          $('.numeric_value').keyup(function() {
            var sum = 0;
            $('.numeric_value').each(function() {
                sum += Number($(this).val());
            });
            $('#total').val(sum);
        });
       
        $(document).ready(function(){
        $("input[data-type='number']").keyup(function(event){
          // skip for arrow keys
          if(event.which >= 37 && event.which <= 40){
              event.preventDefault();
          }
          var $this = $(this);
          var num = $this.val().replace(/,/gi, "");
          var num2 = num.split(/(?=(?:\d{3})+$)/).join(",");
          console.log(num2);
          // the following line has been simplified. Revision history contains original.
          $this.val(num2);
      });
    });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form class="form-inline">
    		<div class="form-group">
    			<input type="number" class="numeric_value" autofocus="autofocus">
    		</div>
    		<div class="form-group">
    			<label>+</label>
    			<input type="number" class="numeric_value">
    		</div>
    		<div class="form-group">
    			<label>=</label>
    			<input type="text" data-type="number" class="form-control" id="total" disabled>
    		</div>
    	</form>
      <!-- <input type="number" class="numeric_value" onkeyup="javascript:this.value=Comma(this.value);"> -->
       <input class="numeric_value" id="id_step2-number_2" name="step2-number_2" type="text" data-type="number">

0 个答案:

没有答案