Keyup event prevents using arrow keys in text field in Chrome

Please check this in the Google Chrome browser:

jQuery('#tien_cong').keyup(function(e) { jQuery(this).val(jQuery(this).val().replace(".", ",")); var sum = 0; var tien_cong = jQuery('#tien_cong').val(); tien_cong = tien_cong.replace(/,/g, ''); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input class="textfield" id="tien_cong" type="text" data-required="" data-type="text" name="tien_cong" placeholder="" value="" size=""> 

I am trying to replace . on , when the user enters something with . in the text box.

In the Chrome browser, when the user presses the left cursor button on the keyboard, he cannot move.

enter image description here

Why?

+6
source share
2 answers

At the moment, the input is updated every time a key is pressed. Testing to see if a character is a '.' before replacing it will prevent the script from starting when it is not needed and prevent the cursor from being reset.

 jQuery('#tien_cong').keyup(function(e) { if(e.which === 190) { jQuery(this).val(jQuery(this).val().replace(/\./g,",")); } var sum = 0; var tien_cong = jQuery('#tien_cong').val(); tien_cong = tien_cong.replace(/,/g, ''); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input class="textfield" id="tien_cong" type="text" data-required="" data-type="text" name="tien_cong" placeholder="" value="" size=""> 
+3
source

Works for me on Chrome 50. But in any case, I recommend changing this:

 jQuery(this).val(jQuery(this).val().replace(/\./g,",")); 

since the rest is always only the first. replaced by.

0
source

All Articles