javascript - jQuery get numeric input from all browsers -
i want user press on number key while focus on input item, , browser should go foo.com/[number]
this code using:
function isnumberkey(evt){ var charcode = (evt.which) ? evt.which : event.keycode; if (charcode != 46 && charcode > 31 && (charcode < 48 || charcode > 57)) return false; return true; } $(document).ready(function(){ $('.my_input').keypress(function(event){ if(isnumberkey(event)){ window.location='foo.com/'+(event.keycode-48); } }); });
it works on google chrome, firefox users getting errors. want implement mobile version user can input number, , press "enter" key of mobile device.
how can ensure work on browsers support javascript?
thanks help!
here's code corrected don't think you're after. note charcode returned numpad different of number-key row.
function isnumberkey(evt){ var charcode = (evt.which) ? evt.which : evt.keycode; if ((charcode >= 48 && charcode <= 57) || (charcode >= 96 && charcode <= 105)) { //0-9 (inc numpad) return true; } return false; } $(document).ready(function(){ $("input[type='number']").on("keyup",function(evt){ if(isnumberkey(evt)){ var charcode = (evt.which) ? evt.which : evt.keycode; //window.location='foo.com/'+(charcode-48); alert('foo.com/'+(charcode-48));//minus 48 wont work if numpad } }); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type="number" value="" />
it may better value
of input instead...
$(document).ready(function(){ $("input[type='number']").on("keyup",function(){ var val = parseint(this.value); this.value=""; if(val>0){ alert('foo.com/'+val); } }); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type="number" value="" />
Comments
Post a Comment