[英]Copy text from input fields and paste into other input fields
Copy button works perfect copy text from first input field and click on paste button paste in it 复制按钮可从第一个输入字段完美复制文本,然后单击粘贴按钮将其粘贴到其中
<input type="text" id="txt1">Some text</input>
<input type="button" value="copy" >
<input type="text" id="txt2">Some text 2</input>
<input type="button" value="copy2" >
<input type="text"></input>
<input type="button" value="paste text" >
Try this solution. 试试这个解决方案。 Add handler to the
copy
buttons and on each click get the previous input
field's value and store in a variable. 将处理程序添加到
copy
按钮,并在每次单击时获取前一个input
字段的值并存储在变量中。 Then in the paste
click set the text into the value of the paste
button. 然后在
paste
将文本设置为paste
按钮的值。
If you have this structure of html, in this way it will be easy to do (with prev()
). 如果您具有html的这种结构,则通过这种方式将很容易做到(使用
prev()
)。
var text = ''; $('.copy').on('click', function(){ text = $(this).prev().val(); }); $('#paste').on('click', function(){ $(this).prev().val(text); });
input { display: block }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type="text" id="txt1" value="Some Text 1"/> <input type="button" class="copy" value="copy" > <input type="text" id="txt2" value="Some Text 2"/> <input type="button" class="copy" value="copy2" > <input type="text"/> <input id="paste" type="button" value="paste text" >
因为您说它运行良好,所以我理解您的问题是那里使用的是哪种语言,既不是javascript也不是jquery,纯html仅用于
var copy_text= '';
$('.copy_button').on('click', function(){
copy_text = $(this).prev().val();
});
$('.paste_button').on('click', function(){
$(this).prev().val(copy_text);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.