I need to make a text field with masked input, and an input mask should change dynamically depending on the first digit of a city code.
Example masks:
+7 ### ### ## ##
+7 #### ## ## ##
By default (while no digits entered), it is possible to use the first mask.
I've tried to do it in this way (with JQuery and Masked Input Plugin):
$("#p_phone_number").mask('+7 999 999 99 99');
$("#p_phone_number").click(function(){
if ( $(this).val().substr(1,4) == "+7 9" ) {
$("#p_phone_number").mask('+7 999 999 99 99');
} else {
$("#p_phone_number").mask('+7 9999 99 99 99');
}
});
But it doesn't work correctly (place digit not under curs开发者_高级运维or, etc.). Can you give me an advice with this situation?
Try changing ...
$("#p_phone_number").click(function(){
... into ...
$("#p_phone_number").keypress(function(){
Click()
should only catch mouse button events, which - by the sound of it - isn't what you want.
精彩评论