简体   繁体   中英

Firing an input changed event when decimals are entered into a number input

The HTML:

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

The JS:

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

The issue is as follows. If the content of the field is 1000 , and the user enters a decimal ( . ), to make it 1000. , it does not fire the input event in Chrome. Is this normal, and if so, then how can I make it fire an event to be able to detect it?

As mentioned in previous comment, the problem is that a number with dot "." in the end doesn't passed in. So here is something you can do:

 $(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"/> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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