jQuery onchange sum不能限制输入类型号

时间:2018-10-18 05:20:44

标签: javascript jquery laravel foreach sum

我使用脚本对输入值求和,并且可以正常工作。但是,当我单击上下时,“ input#total_selfmark”不会更改,并且当我键入数字时,“ input#total_selfmark”会更改,并且数字没有限制。 甚至我设置了min = 0 max = $ task_criteria-> maximummark

     <div class="form-group">
{{ Form::number('selfmark','',['placeholder'=>'', 'class' =>'selfmark', 'min'=>'0','max'=>$task_criteria->maximummark]) }}
       </div>

这是脚本

$(document).ready(function() {
  //this calculates values automatically
  calculateSum();
  $(".selfmark").on("keydown keyup", function() {
    calculateSum();
  });
});

function calculateSum() {
  var sum = 0;

  //iterate through each textboxes and add the values
  $(".selfmark").each(function() {
    //add only if the value is number
    if (!isNaN(this.value) && this.value.length != 0) {
      sum += parseFloat(this.value);
      $(this).css("background-color", "#FEFFB0");
    } else if (this.value.length != 0) {
      $(this).css("background-color", "red");
    }
  });

  $("input#total_selfmark").val(sum.toFixed(2));
}
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
We miss some HTML here!

2 个答案:

答案 0 :(得分:2)

我正在猜测您的HTML,还假设您故意使用了旧的jQuery版本

$(document).ready(function() {
  $(".selfmark").on("input", calculateSum).trigger("input");    });

function calculateSum() {
  var sum = 0;
  $(".selfmark").each(function() { // or map/reduce
    var val = this.value, max = +$(this).attr("max"), bg = "#FEFFB0";
    if (isNaN(val) || val.length == 0 || val>max) {
      bg= "red";
    }    
    else {
      sum += parseFloat(val);
    } 
    $(this).css("background-color", bg);
  });

  $("input#total_selfmark").val(sum.toFixed(2));
}
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
0-10: <input type="number" min="0", max="10" class="selfmark" value="5" /><br/>
0-20: <input type="number" min="0", max="20" class="selfmark" value="0" /><br/>
0-30: <input type="number" min="0", max="30" class="selfmark" value="0"/><br/>
<input type="text" readonly id="total_selfmark" />

替代方法-不短,但使用fn.map和reduce

$(document).ready(function() {
  $(".selfmark").on("input", calculateSum).trigger("input");
});

function calculateSum() {
  var sum = $(".selfmark").map(function(idx, ele) {
    var val = this.value, max = +$(this).attr("max"), bg = "#FEFFB0";
    if (isNaN(val) || val.length == 0 || val > max) {
      bg = "red";
      val = 0;
    } 
    else val = +val;
    $(this).css("background-color", bg);
    return val;
  }).get().reduce(function(a, b) { return a + b; }, 0); // sum the array
  $("input#total_selfmark").val(sum.toFixed(2));
}
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
0-10: <input type="number" min="0", max="10" class="selfmark" value="5" /><br/>
0-20: <input type="number" min="0", max="20" class="selfmark" value="0" /><br/>
0-30: <input type="number" min="0", max="30" class="selfmark" value="0"/><br/>
<input type="text" readonly id="total_selfmark" />

答案 1 :(得分:0)

$(document).ready(function() {
  const $selfmarks = $(".selfmark");
  $selfmarks.on("input", calculateSum);
  $selfmarks.trigger("input");

  function calculateSum(e) {
    e.target.style.background = e.target.value === '' ? 'red' : 'transparent';
    e.target.value = Number(e.target.value) > 10 ? 10 : e.target.value
    
    const sum = Array.from($selfmarks).reduce(function (acc, $cur) {
      acc += Number($cur.value || 0)
      return acc
    }, 0);

    $("input#total_selfmark").val(sum.toFixed(2));
  }
});
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
<input type="number" min="1" max="10" class="selfmark" /><br/>
<input type="number" min="1" max="10" class="selfmark" /><br/>
<input type="number" min="1" max="10" class="selfmark" /><br/>
<input type="text" readonly id="total_selfmark" />