Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/373.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 将输入字段限制为280个字符_Javascript_Textbox - Fatal编程技术网

Javascript 将输入字段限制为280个字符

Javascript 将输入字段限制为280个字符,javascript,textbox,Javascript,Textbox,我正在尝试将文本框输入字段限制为280个字符,以便与twitter共享。cap本身可以工作,但由于某种原因,当文本被复制并粘贴到文本框中时,告诉您还有多少字符的计数器的行为似乎非常奇怪。如果我直接输入,它可以正常工作,但一旦我粘贴,如果它达到限制,然后我删除任何文本,字符数将不正确,我无法找出原因 function twitterMessageRemaining(message) { const twitterMaxValue = 280; if (!me

我正在尝试将文本框输入字段限制为280个字符,以便与twitter共享。cap本身可以工作,但由于某种原因,当文本被复制并粘贴到文本框中时,告诉您还有多少字符的计数器的行为似乎非常奇怪。如果我直接输入,它可以正常工作,但一旦我粘贴,如果它达到限制,然后我删除任何文本,字符数将不正确,我无法找出原因

    function twitterMessageRemaining(message) {
        const twitterMaxValue = 280;
        if (!message) message = '';
        var len = 0;
        var cleanMessage = message.replace(
            /\$AMF_(?:SHORT_)?PERMALINK\$?|https?:\/\/\S+/gi,
            function (foo) { len += 20; return ''; }
        );
        len += cleanMessage.length;

        // default message to 0 if text is inserted longer than
        // maxTwitterValue as that text is deleted automatically
        if (message.length > twitterMaxValue) return 0;
        else return twitterMaxValue - len;
    }

    var FieldTwitter = function (field) { FieldText.call(this, field); };
    $.extend(FieldTwitter.prototype, FieldText.prototype);
    FieldTwitter.prototype.render = function () {
        this.input = Tag('textarea', {
            name:   this.name,
            value:  this.default,
            rows:   5,
            cols:   40
        });

        var container = wrapLabel(this.label, this.input);

        // Twitter counter
        var counter = Tag('span', twitterMessageRemaining(this.default));
        $(counter).css({ 'margin-left': '10px' });

        $(this.input).keyup(function () {
            var len = twitterMessageRemaining(this.value);
            counter.innerHTML = len;
        });

        // if the text inputted excedes the maximum allowed length by twitter
            // delete any text overflow
            if (this.value.length > twitterMaxValue) {
                this.value = this.value.substr(0, twitterMaxValue);
                return false;
            }

        container.appendChild(counter);
        return container;
    };

请在问题中添加相关的html