如何使用jquery在textarea中的每一行复制文本

时间:2015-03-11 03:06:51

标签: javascript jquery

我想使用jquery ..

在textarea中的每一行复制文本

<!--
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!" />

我希望你能帮助我们,谢谢你们。

2 个答案:

答案 0 :(得分:0)

您可以执行类似

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

演示:Fiddle

答案 1 :(得分:0)

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

  

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

&#13;
&#13;
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;
}
&#13;
<textarea id="input" style="width:100%; height:100px;" placeholder="input" ></textarea>
<textarea id="output" style="width:100%; height:100px;" placeholder="output" ></textarea>
&#13;
&#13;
&#13;

  

更新: jQuery方法

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

我希望这会有所帮助。快乐的编码!