Jquery如何在ajax成功响应后自动复制剪贴板上的输入字段值

时间:2016-11-25 04:33:09

标签: jquery html ajax

我所取得的成就是我可以使用Clipboard.js复制点击事件的链接 但我也想在ajax成功响应后自动复制剪贴板上的输入字段值。 如何实现这一功能?

1 个答案:

答案 0 :(得分:1)

您可以使用 document.execCommand('copy')



function copyText() {
  var input1 = document.getElementById('txt');
  input1.select();
  document.execCommand('copy')
}

<input id='txt' required />

<input type="submit" value="copy" onclick="copyText()" />
<br>
<br>
<textarea placeholder="Paste it here"></textarea>
&#13;
&#13;
&#13;

您的需求:

(来自评论中提及的小提琴)

$.ajax({
   type: 'POST',
   url: url,
   async: false,
   data: data
   success: function(response) {
     $("#shortlink").val(response);
     $('#shortlink').select();
     document.execCommand('copy')
   },
   error: function(textStatus, errorThrown) {

   }
 });