IE 中文本区域最大长度

发布于 2024-12-27 16:05:57 字数 966 浏览 4 评论 0原文

有没有办法从给定的 keydown 事件判断所有者 input[type=text]textarea 的长度在之后会减少还是增加它被应用了吗?


背景:有一个 HTML5 网站 (),必须支持 IE 8 和 9 用户作为主要消费者。在某些页面上,我有带有 maxlength="100" 属性的

我还没有看到一个垫片可以限制 IE 中 textarea 的字符长度,其键盘行为与 input[type=text] 元素相同。

出于这个原因,我正在尝试写一篇。

现有的实现看起来像这样:

$('textarea[maxlength]').on('keydown keyup blur', function(e) {
    var $this = $(this),
        maxlen = $this.attr('maxlength'),
        val = $this.val();

    if (val.length > maxlen) {
        $this.val(val.substr(0, maxlen));
    }
});

上述实现的问题是您可以键入或粘贴 11 个字符和值直到值更改后才将其修剪为最大长度。我想在值更改之前阻止更改,以便更好地与 input[type=text,maxlength] 行为保持一致。

Is there any way to tell from a given keydown event whether the owner input[type=text] or textarea will decrease or increase in length after it's applied?

Background: have an HTML5 site (<!DOCTYPE html>) that has to support IE 8 and 9 users as the primary consumer. On some pages, I have <textarea> elements with maxlength="100" attributes. IE does not recognize the maxlength attribute, and allows more than 100 characters to be entered into the textarea.

I have not seen a shim that limits the character length of a textarea in IE with the same keyboard behavior as a input[type=text] element.

For that reason, I'm trying to write one.

Existing implementations look something like this:

$('textarea[maxlength]').on('keydown keyup blur', function(e) {
    var $this = $(this),
        maxlen = $this.attr('maxlength'),
        val = $this.val();

    if (val.length > maxlen) {
        $this.val(val.substr(0, maxlen));
    }
});

The problem with above implementation is that you can type or paste 11 characters and the value is not trimmed to the maxlength until after the value has changed. I would like to prevent a change before the value changes, to better align with input[type=text,maxlength] behavior.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

眼泪淡了忧伤 2025-01-03 16:05:57

您已经在监听 keydown 和 keyup 事件 - 不幸的是,您无能为力。按键事件可能会添加更多内容,但我认为它们会与按键一起触发,所以可能不会。

上述代码的一个问题是,如果您右键单击并粘贴(或通过编辑菜单执行此操作),则它将无济于事。

一种可能的解决方法是使用 CSS 将文本区域隐藏在屏幕之外,然后放置另一个模仿它的“假”文本区域。当用户键入时,让它在屏幕外的操作中执行操作,然后将所需内容复制到屏幕上的“假”操作中。在实践中,我不确定这对于像右键单击> >这样的实例如何工作。粘贴情况 - 可能需要大量的混乱。

You're already listening on keydown and keyup events - there's nothing else you can do, unfortunately. keypress events might add something more, but I think they trigger along with keyup so probably not.

One issue with the above code is that if you right-click and paste (or do so through the edit menu) then it won't help.

One possible workaround is to use CSS to hide the textarea off-screen, then put another "false" textarea which mimics it in place. When the user types, have it action in the off-screen one, and then copy what you want to the "false" one on-screen. In practice, I'm not sure how this would work with instances like the right-click > paste situation - it might take a lot of messing around.

纸短情长 2025-01-03 16:05:57

您的问题是您的 Internet Explorer 甚至没有将 maxlength 的值放入 html 中,例如尝试将其硬编码到 javaScript 中;

 $(document).on('input keyup', 'textarea', function(e) {
        // maxlength attribute does not in IE prior to IE10
        // http://stackoverflow.com/q/4717168/740639
        console.debug("here")
        var $this = $(this);
        var maxlength = 255;
        if (!!maxlength) {
            var text = $this.val();

            if (text.length > maxlength) {
                // truncate excess text (in the case of a paste)
                $this.val(text.substring(0,maxlength));
                e.preventDefault();
            }

        }

    });

Your problem is that your internet explorer doesn't even put the value of maxlength into html, try to hard code that into javaScript, for example;

 $(document).on('input keyup', 'textarea', function(e) {
        // maxlength attribute does not in IE prior to IE10
        // http://stackoverflow.com/q/4717168/740639
        console.debug("here")
        var $this = $(this);
        var maxlength = 255;
        if (!!maxlength) {
            var text = $this.val();

            if (text.length > maxlength) {
                // truncate excess text (in the case of a paste)
                $this.val(text.substring(0,maxlength));
                e.preventDefault();
            }

        }

    });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文