简体   繁体   English

计算Textarea字符并增加或减小TextArea的大小

[英]Count Textarea Character and increase or Decrease size of the TextArea

I want to allow user to enter upto 300 character and simultaneously. 我想允许用户同时输入最多300个字符。 I want to increase textarea based on user text if he deletes text, decrease textarea 如果他删除文本,我想根据用户文本增加textarea,减少用户textarea

https://jsfiddle.net/yhqdhr3x/ https://jsfiddle.net/yhqdhr3x/

 <div id="divChar">300</div>
 <textarea id="txtpostSearch" class="form-control" rows="3" ></textarea>

JQuery Code jQuery代码

$('#txtpostSearch').keyup(function () {                
    var txtCharCountLength = $(this).val().length;
    if (txtCharCountLength <= 300) {
        var remainingChar = 300 - txtCharCountLength;
        $("#divChar").html(remainingChar);
    }
});

In your initialisation, put the following to prevent errors (this is for what keeps it less than 300 chars): 在初始化时,放置以下内容以防止错误(这是为了使它少于300个字符):

oldVal = "";

If you use a monospace font, and know the number of chars that fit in a row, each time the letter is typed, you could do the following on each character typed.... this assumes you can fit 22 chars on each row... 如果您使用等宽字体,并且知道适合行的字符数,则每次键入字母时,都可以对键入的每个字符执行以下操作。...这假设您可以在每行中容纳22个字符。 ..

// save the element as variable, just to access without typing as much
elem = document.getElementById('txtpostSearch');

//if the length is above 300, set it back to what the value was before
if(elem.length > 300){
    elem.value = elem.value.substring(0, 300);
}

// the length of the contents of the textarea
len = elem.value.length;
// the new number of rows in the textarea
// it is the number of rows completely filled by text, plus two
// plus two makes room for the row currently typing in, and one more empty row
rows = Math.floor(len / 22) + 2;
// sets the height as the number of rows in units of font size
elem.style.height = rows + 'em';

//store the value in case the next length is too large
oldVal = elem.value;

I made a little fiddle: https://jsfiddle.net/5w7gogqt/ 我做了一些小提琴: https : //jsfiddle.net/5w7gogqt/

This fiddle uses a font called ProFont (if you dont have it, the fiddle defaults to plain monospace), which always has a set size no matter what OS or browser you use. 该小提琴使用一种称为ProFont的字体(如果没有,默认为纯等宽字体),无论您使用什么操作系统或浏览器,该字体的大小始终是固定的。 You can always use a webfont for your field, then people use that font even if they don't have it. 您始终可以为您的字段使用Web字体,然后人们即使没有字体也可以使用该字体。

You can change the number of rows and columns of the text area using JQuery. 您可以使用JQuery更改文本区域的行数和列数。

To limit the number of input characters there's maxlength but it doesn't work consistently on all browsers, as an alternative you can just truncate the content of the textbox with substring and change the value of the textbox. 要限制输入字符的数量,可以使用maxlength但是它不能在所有浏览器上都一致地工作,作为替代,您可以使用substring截断文本框的内容并更改文本框的值。

Demo: 演示:

 $('#txtpostSearch').keyup(function() { var str = $(this).val(); var txtCharCountLength = str.length; if (txtCharCountLength <= 300) { var remainingChar = 300 - txtCharCountLength; $("#divChar").html(remainingChar); var rows = str.split("\\n"); var cols = rows.sort(function(a, b) { return b.length - a.length })[0].length; $(this).attr('rows', rows.length + 1); $(this).attr('cols', cols); } else { $(this).val($(this).val().substring(0, 300)); } }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="divChar">300</div> <textarea runat="server" class="form-control" cows="3" id="txtpostSearch"></textarea> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM