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



我的成就是我可以通过使用clipboard.js在单击事件上复制链接但是我也想在AJAX成功响应后自动复制剪贴板上的输入字段值。如何实现此功能?

您可以使用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>

为您的req:

(来自您在评论中提到的小提琴)

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

最新更新