第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

限制textarea中的行數(shù)并使用jQuery顯示行數(shù)

限制textarea中的行數(shù)并使用jQuery顯示行數(shù)

我想使用jQuery:將用戶(hù)可以在文本區(qū)域中輸入的行數(shù)限制為設(shè)置的數(shù)字使行計(jì)數(shù)器出現(xiàn),以在輸入行時(shí)更新行數(shù)回車(chē)鍵或/ n將計(jì)為行感謝任何可以幫助的人!$(document).ready(function(){  $('#countMe').keydown(function(event) {    // If number of lines is > X (specified by me) return false    // Count number of lines/update as user enters them turn red if over limit.  });   });<form class="lineCount">  <textarea id="countMe" cols="30" rows="5"></textarea><br>  <input type="submit" value="Test Me"></form><div class="theCount">Lines used = X (updates as lines entered)<div>對(duì)于此示例,可以說(shuō)將允許的行數(shù)限制為10。謝謝大家!
查看完整描述

3 回答

?
jeck貓

TA貢獻(xiàn)1909條經(jīng)驗(yàn) 獲得超7個(gè)贊

這是很少的改進(jìn)代碼。在前面的示例中,您可以粘貼帶有更多行的文本。


的HTML


<textarea data-max="10"></textarea>

<div class="theCount">Lines used: <span id="linesUsed">0</span></div>

JS


jQuery('document').on('keyup change', 'textarea', function(e){


        var maxLines = jQuery(this).attr('data-max');        

        newLines = $(this).val().split("\n").length;


        console.log($(this).val().split("\n"));


        if(newLines >= maxLines) {

            lines = $(this).val().split("\n").slice(0, maxLines);

            var newValue = lines.join("\n");

            $(this).val(newValue);

            $("#linesUsed").html(newLines);

            return false;

        }


    });


查看完整回答
反對(duì) 回復(fù) 2019-12-02
?
喵喵時(shí)光機(jī)

TA貢獻(xiàn)1846條經(jīng)驗(yàn) 獲得超7個(gè)贊

一個(gè)非常丑陋但以某種方式起作用的示例指定了textarea行


<textarea rows="3"></textarea>

然后在js中


   $("textarea").on('keydown keypress keyup',function(e){

       if(e.keyCode == 8 || e.keyCode == 46){

           return true;

       }

       var maxRowCount = $(this).attr("rows") || 2;

        var lineCount = $(this).val().split('\n').length;

        if(e.keyCode == 13){

            if(lineCount == maxRowCount){

                return false;

            }

        }

        var jsElement = $(this)[0];

        if(jsElement.clientHeight < jsElement.scrollHeight){

            var text = $(this).val();

            text= text.slice(0, -1);

            $(this).val(text);

            return false;

        }


    });


查看完整回答
反對(duì) 回復(fù) 2019-12-02
  • 3 回答
  • 0 關(guān)注
  • 1702 瀏覽
慕課專(zhuān)欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢(xún)優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)