【问题标题】:onpaste event on asp.net textbox in internet explorer not workingInternet Explorer中asp.net文本框上的onpaste事件不起作用
【发布时间】:2017-04-13 11:33:37
【问题描述】:

这是我的文本框:

<asp:TextBox ID="txtBoxFlagDes" TextMode="MultiLine" runat="server" MaxLength="30" Width="150px" onkeypress="return this.value.length<=30"></asp:TextBox>

这是脚本:

<script src="../Scripts/jquery-3.1.1.min.js" type="text/javascript"></script>
<script type="text/javascript">
    $(document).ready(function () {
        $("#txtBoxFlagDes").on("input", function () {
            LimtCharacters(this, 30);
        });
    });
    function LimtCharacters(txtMsg, CharLength) {
        chars = txtMsg.value.length;
        if (chars > CharLength) {
            txtMsg.value = txtMsg.value.substring(0, CharLength);                
        }
    }
</script>

当用户复制一些文本并粘贴到文本框中时,它会从剪贴板中取出整个文本,简而言之,脚本不起作用。没遇到问题

【问题讨论】:

    标签: javascript jquery asp.net copy-paste


    【解决方案1】:

    根据Browser's compatibility of input event,它不支持IE

    Feature             Chrome     Firefox     Internet Explorer    ....
    Basic support       (Yes)      2           9                     
    

    【讨论】:

    • 是的,但我使用的是 IE 11,它在任何浏览器中都不起作用
    【解决方案2】:

    您的脚本正在检查用户或按键事件的输入。但是当复制文本粘贴到测试框中时,此事件不会触发。所以很明显你的 scipt 在这种情况下是行不通的。

    可能在文本框中的文本之后,您可以验证失去焦点的文本。

    希望这会有所帮助。

    【讨论】:

    • 您能提供其他解决方案吗
    • 它在我的网站上不起作用,显示以下错误:localhost:59771/Surviellance/GSMFlagMaster.aspx 0x800a138f 第 33 行第 9 列未处理的异常 - JavaScript 运行时错误:无法设置未定义或空引用的属性“onpaste”
    • 在这种情况下,您的 javascript 无法识别服务器端控件 ID。您是否可以在脚本中使用控件的客户端 ID。以这种方式,一旦控制将被识别,这个错误应该被解决。希望这会有所帮助。
    • &lt;script type="text/javascript" language="javascript"&gt; var test = document.getElementById("&lt;%= txtBoxFlagDes.ClientID %&gt;"); test.onPaste = function (e) { var max = test.getAttribute("maxlength"); e.clipboardData.getData('text/plain').slice(0, max); }; &lt;/script&gt; 这是我正在使用的代码,但测试也得到了空引用
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-30
    • 1970-01-01
    • 1970-01-01
    • 2013-04-30
    • 1970-01-01
    相关资源
    最近更新 更多