简体   繁体   English

Javascript - 从 sweetalert2 按钮复制确认

[英]Javascript - copy from sweetalert2 button confirm

how can I copy from my button "Copy URLs" when is clicked?单击时如何从“复制 URL”按钮复制?

function cdwUrlsMDBE() {
            var prd = document.getElementById("prd");

            if (prd.checked == true) {
                Swal.fire({
                    type: 'success',
                    html: 'i am a text that should be copy',
                    width: 'auto',
                    confirmButtonText: 'Copy URLs',
                })

Thanks谢谢

You could do it creating a button via html and Bootstrap , in this way you could attach an handler on the button via onOpen .您可以通过htmlBootstrap创建一个按钮,这样您就可以通过onOpen在按钮上附加一个处理程序。

Something like this像这样的东西

Swal.fire({
    title: "Copy it!",
    html: 
        '<textarea id="text_to_be_copied" class="swal2-input" readonly>Some text you want to copy</textarea>' +
            '<button type="button" class="btn btn-default" id="btn-copy" style="margin-left:5px">Copy</button>' +
            '<button type="button" class="btn btn-primary swal-confirm" id="btn-ok" style="float:right" disabled>Text have been copied!</button>' +
        '</div>',
    showConfirmButton: false,
    type: "success",
    onOpen: () => {
        $("#btn-copy").click(() => {
            $("#btn-ok").prop('disabled', false);

            $("#text_to_be_copied").select();
            document.execCommand("copy");
        });

        $("#btn-ok").click(() => {
            Swal.close();   
        });
    }
});

In this way the "confirmation button" is displayed only after you use the copy one, but you could decide to get rid of that just by putting Swal.close() inside the handler of the copy button.通过这种方式,“确认按钮”仅在您使用复制按钮后才显示,但您可以决定通过将Swal.close()放在复制按钮的处理程序中来摆脱它。 In this way after clicking che copy button, the popUp will close.这样点击复制按钮后,弹出窗口就会关闭。

You could choose to use also an input tag instead of a textarea depends on your needs.根据您的需要,您还可以选择使用输入标签而不是文本区域。

var txtToBeCopied ='i am a text that should be copy';

    if (prd.checked == true) {
                Swal.fire({
                    type: 'success',
                    html: txtToBeCopied,
                    width: 'auto',
                    confirmButtonText: 'Copy URLs',
                },
 function(isConfirm){

   if (isConfirm){

     /* Get the text field */
  var copyText = txtToBeCopied;

  /* Select the text field */
  copyText.select();
  copyText.setSelectionRange(0, 99999); /*For mobile devices*/

  /* Copy the text inside the text field */
  document.execCommand("copy");


    }
 })
 }

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

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