开发者

JQuery masked input question

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜