2016-02-12 34 views
0

我想制作一个动态遮罩,因此如果文本框长度中的数字大于11,它应该是不同的遮罩。为什么我得到这个输入错误?

我有以下代码:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm3.aspx.cs"  Inherits="WebApp_MVP.WebForm3" %> 

<!DOCTYPE html> 

<html xmlns="http://www.w3.org/1999/xhtml"> 
<head runat="server"> 
    <title></title> 
    <script src="scripts/jquery-2.2.0.min.js"></script> 
    <script src="scripts/jquery.maskedinput.js"></script> 
    <script type="text/javascript"> 

    jQuery(function ($) { 
     $("#TextBoxDoc").keydown(function() { 
      try { 
       $("#TextBoxDoc").unmask(); 
      } catch (e) { } 

     var tamanho = $("#TextBoxDoc").val().length; 

     if (tamanho < 11) { 
      $("#TextBoxDoc").mask("999.999.999-99"); 
     } else if (tamanho >= 11) { 
      $("#TextBoxDoc").mask("99.999.999/9999-99"); 
     } 
     }); 
    }); 

</script> 
</head> 
<body> 
    <form id="form1" runat="server"> 
     <div> 
      <asp:TextBox runat="server" ClientIDMode="Static" ID="TextBoxDoc"><asp:TextBox> 
     </div> 
    </form> 
</body> 
</html> 

的问题是,当我试图写文档数量,只保留一个字符,并在不断变化,最后一个是这样的:

[______________] *按压3

[3_____________] *压力机2

[2_____________] * p resses 9

[9_____________] ...等

+1

你是什么意思它使一个并改变了最后一个? –

+0

当我按下一个号码时,会删除最后一个号码,例如: [] * press 3 [3 __.___.___-__] * press 2 [2 _.___.___/____-__] *按9 [9 __________/____-__] 等等... –

回答

0

而不是使用揭露和remask(我相信是删除您的信息)的尝试“对飞”上的更改使用。

var options = {onKeyPress: function(cep, e, field, options){ 
    var masks = ['999.999.999-99', '99.999.999/9999-99']; 
    mask = (cep.length > 11) ? masks[1] : masks[0]; 
    $('#TextBoxDoc').mask(mask, options); 
}}; 

$('#TextBoxDoc').mask('999.999.999-99', options); 

看到https://igorescobar.github.io/jQuery-Mask-Plugin/

+0

我试过这个,但它在第一个掩码中保持静态,我没有机会添加一个数字,所以掩码将会更改 –

+0

你从哪里得到jquery.maskedinput.js文件? –

+0

https://plugins.jquery.com/maskedinput/ –