简体   繁体   中英

Keyup event prevents arrow keys in text field in Chrome

Please check this on 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 try to replace . by , when user types somethings with . in a textbox.

On Chrome browser, when user press left cursor button on keyboard , it can not move.

在此输入图像描述

Why?

Right now the input is updating every time a key is pressed. Testing to see if the character is a '.' before replacing will prevent the script from running when it doesn't need to, and prevents the cursor from resetting.

 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=""> 

Works for me on Chrome 50. But anyway I recommend to change this:

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

as otherwise always only the first '.' gets replaced.

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