当小数输入数字输入时,触发输入更改事件

时间:2016-01-06 16:06:17

标签: javascript jquery html google-chrome

HTML:

<input id="example" type="number"/>

JS:

$("#example").on("input", function (e) { 
    debugger;
});

问题如下。如果该字段的内容为1000,并且用户输入小数(.),并将其设为1000.,则不会触发Chrome中的input事件。这是正常的,如果是这样,那么我怎样才能使事件能够发现它?

1 个答案:

答案 0 :(得分:1)

如前面的评论所述,问题是带有“。”的数字。最后没有传入。所以这是你可以做的事情:

$(function(){
  var lastNum;
  $("#example").on("input", function (e) {
    if(this.value.length>0)lastNum=this.value;//if value passed in correctly - update it
    console.debug(lastNum);//Do anything with the lastNum
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input id="example" type="number"/>