2016-02-12 48 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/ –

相關問題