简体   繁体   中英

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. Then in the paste click set the text into the value of the paste button.

If you have this structure of html, in this way it will be easy to do (with 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);
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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