简体   繁体   English

在提交表格之前阅读textarea中的新行?

[英]Read new-lines in textarea BEFORE submitting form?

I have an textarea with attribute wrap="hard", meaning I should be able to get the line break (new lines) after submit to server - this works. 我有一个textarea属性wrap =“hard”,这意味着我应该能够在提交给服务器后获得换行符(新行) - 这是有效的。 But what I wanna do is get the new lines that are created before submission. 但我想做的是获取提交之前创建的新行。

Why? 为什么? Because I wanna count the rows that are currently visible. 因为我想要计算当前可见的行数。 And now I'm NOT talking about carriage return rows. 现在我不是在谈论回车行。 I'm talking about rows that are created by limiting the width (or setting the cols attribute) of the textarea. 我在谈论通过限制textarea的宽度(或设置cols属性)创建的行。

I have this textbox: 我有这个文本框:

<textarea id="myarea" name="myarea" cols="35" rows="10" wrap="hard">
  Some example text here. Hello my name is something I should be able to blabla.
</textarea>

Output in browser: 浏览器输出:
Some example text here. 这里有一些示例文本。 Hello my name is 你好我的名字是
something I should be able to blabla. 我应该能够克拉拉的东西。

rows = 2 rows = 2

I've tried: 我试过了:
$('#myarea').html().split("\\n").length $( '#myarea')。HTML()。分裂( “\\ n”)。长度
$('#myarea').val().split("< br>").length $('#myarea')。val()。split(“<br>”)。长度
$('#myarea').val().split("\\r").length $( '#myarea')。VAL()。分裂(为 “\\ r”)。长度
And a few more combinations... 还有一些组合......

But none works. 但都没有效果。 Is what I'm asking even possible without writing a script that counts each character and then creates a newline? 在没有编写计算每个字符的脚本然后创建换行符的情况下,我甚至可以问这个问题吗? I was hoping this would happend "automatically"... 我希望这会“自动”发生......

If this is not possible, why can the server interpret (find) the new lines, while I can not? 如果这是不可能的,为什么服务器可以解释(找到)新行,而我不能?

Thanks! 谢谢!

Seems like there is no build in tools to get this value. 似乎没有用于获得此值的工具的构建。 But we can calculate it. 但我们可以计算出来。 Assume that the line-height in textarea is a known value (you can explicitly define it in css). 假设textarea中的行高是一个已知值(您可以在css中明确定义它)。 So the code to calculate the number of rows would like this: 所以计算行数的代码是这样的:

    var area = $('#myarea').get(0);

    //save the initial height of textarea
    var initialHeight = area.style.height;

    //set the height to 0 so scrollHeight will always return dynamic height
    area.style.height = '0px';

    //here we assume that the line-height equals to 15
    var numberOfRows = Math.floor(area.scrollHeight / 15);

    //restore textarea height
    area.style.height = initialHeight;

    console.log(numberOfRows);

Working example http://jsfiddle.net/J5UpF/ 工作示例http://jsfiddle.net/J5UpF/

UPDATE UPDATE

Just figured out the bug in Chrome with my code. 刚用我的代码弄清楚Chrome中的错误。 I didn't take into account the scrollbar that appears on the right side of textarea. 我没有考虑textarea右侧出现的滚动条。 That leads to the wrong number of lines calculated sometimes. 这导致有时计算的行数错误。 However it works like a charm in Firefox. 然而它就像Firefox中的魅力一样。 To fix this issue we have to calculate the difference of scrollbars width and adjust the width of textarea accordingly: 要解决此问题,我们必须计算滚动条宽度的差异并相应地调整textarea的宽度:

var area = $('#myarea').get(0);
var $area = $('#myarea');

//save the initial height of textarea
var initialHeight = area.style.height;

var scrollbarWidthInitial = area.offsetWidth - area.clientWidth;

//set the height to 0 so scrollHeight will always return dynamic height
area.style.height = '0px';

var scrollbarWidth = area.offsetWidth - area.clientWidth;
var scrollbarWidthDiff = scrollbarWidth - scrollbarWidthInitial;
$area.width($area.width() + scrollbarWidthDiff);

//here we assume that the line-height equals to 15
var numberOfRows = Math.floor(area.scrollHeight / 15);

//restore textarea height
area.style.height = initialHeight;
$area.width($area.width() - scrollbarWidthDiff);

console.log(numberOfRows);

updated example http://jsfiddle.net/J5UpF/3/ 更新示例http://jsfiddle.net/J5UpF/3/

UPDATE 2 更新2

A new awesome bug in Chrome is discovered! Chrome中发现了一个令人敬畏的新bug! When using placeholder attribute Chrome calculates the number of rows for placeholder text. 使用placeholder属性时,Chrome会计算占位符文本的行数。 The workaround is simple: just backup the placeholder attribute when you entered some data into textarea and restore it when data is cleared. 解决方法很简单:只需在将一些数据输入textarea时备份占位符属性,并在清除数据时将其恢复。

Updated example http://jsfiddle.net/J5UpF/4/ 更新示例http://jsfiddle.net/J5UpF/4/

您可以在onClick上的提交按钮上绑定jquery,然后使用

$('#myarea').val().split("\n").length

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

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