为什么我会收到此输入错误?

Why Am I getting this Input Error?

我想制作一个动态掩码,所以如果文本框长度中的数字超过 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>

问题是,当我尝试写文档编号时,它只保留一个字符,并不断更改最后一个字符,如下所示:

[______________] *presses 3

[3_____________] *presses 2

[2_____________] *presses 9

[9_____________] and so on...

不要使用取消屏蔽和重新屏蔽(我认为这会删除您的信息)尝试使用 'on the fly' 屏蔽更改。

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/