简体   繁体   English

jQuery-单击以将父元素复制到文本字段

[英]Jquery - Click to copy parent element to text field

I want 4 buttons next to each URL, you can then click on a button to copy the parent url to the buttons respective textfield. 我希望每个URL旁边有4个按钮,然后可以单击一个按钮将父URL复制到按钮各自的文本字段中。

I presume that it is easiest to do this using jquery but I have no clue where to start. 我认为使用jquery进行此操作最简单,但是我不知道从哪里开始。 Any advice would be much appreciated! 任何建议将不胜感激!

http://jsfiddle.net/WwdMw/ http://jsfiddle.net/WwdMw/

<div class="selection">
  <div class"url">
    <h2>example1.com</h2>
  </div>
  <button type="button" class="btn_opt1">Copy to Option1</button>
  <button type="button" class="btn_opt2">Copy to Option2</button>
  <button type="button" class="btn_opt3">Copy to Option3</button>
  <button type="button" class="btn_opt4">Copy to Option4</button>
</div>
<hr/>
<div class="selection">
  <div class"url">
    <h2>example3.com</h2>
  </div>
  <button type="button" class="btn_opt1">Copy to Option1</button>
  <button type="button" class="btn_opt2">Copy to Option2</button>
  <button type="button" class="btn_opt3">Copy to Option3</button>
  <button type="button" class="btn_opt4">Copy to Option4</button>
</div>
<hr/>

<div class="selection">
  <div class"url">
    <h2>example2.com</h2>
  </div>
  <button type="button" class="btn_opt1">Copy to Option1</button>
  <button type="button" class="btn_opt2">Copy to Option2</button>
  <button type="button" class="btn_opt3">Copy to Option3</button>
  <button type="button" class="btn_opt4">Copy to Option4</button>
</div>

<hr/>

Have it. 有它。

$('button').click(function () {
    var value = $(this).parent().find('h2').text();
    var idIdentifier = $(this).text().substr($(this).text().length - 1);
    var textArea = $('div[class*='+idIdentifier+']').find('textarea');
    textArea.val(textArea.val() +" "+ value);
})

Live Demo 现场演示

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

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