如何使用jquery复制文本区域中的每一行文本

How to duplicate text every line in textarea using jquery

本文关键字:文本 一行 jquery 何使用 复制 区域      更新时间:2023-09-26

我想使用jquery复制文本区域中的每一行文本。

<!--
first, im gonna paste my text in input like this..
<textarea id="input">
one
two

three
four
five
six
seven
(and so on...)
</textarea>
and the result is like this..
<textarea id="Result">
one
one
two
two
two
two
two
two
two
three
three
four
four
four
five
five
five
five
six
six
six
seven
seven
seven
seven
</textarea>
duplicating text is depend on break line..
-->
here is my code:
<textarea id="input" style="width:100%; height:100px;" placeholder="input" ></textarea>
<textarea id="output" style="width:100%; height:100px;" placeholder="output" ></textarea>
<input type="button" value="Process!" />

我希望你能帮助我伙计们,提前谢谢..

你可以

做类似的事情

var invalue = $('#input').val();
$('#output').val(invalue.replace(/^(.*)$/gm, '$1'n$1'))

演示:小提琴

由于您在问题中标记了javsascript,我想我会在不使用jQuery的情况下为您提供演示。

有兴趣使用此方法,请发表评论,我将解释您对此方法不了解的任何内容。

window.onload=function(){
	var a=document.getElementById('input');
    a.addEventListener('input',MyFunction,false);
    a.focus();
}
function MyFunction(){
	var DataIn=event.target.value;
	DataIn = DataIn.replace(/('n){1,}/g, ''n');
	document.getElementById('output').value=DataIn;
}
<textarea id="input" style="width:100%; height:100px;" placeholder="input" ></textarea>
<textarea id="output" style="width:100%; height:100px;" placeholder="output" ></textarea>

更新:j查询方法

$('#input').keyup(function(){
    $('#output').val($(this).val().replace(/('n){1,}/g, ''n'));
});

我希望这有所帮助。祝您编码愉快!