jaavscript-如何防止两次警报?

时间:2019-03-21 09:23:58

标签: javascript

我有一个特殊的情况,我的页面看起来像这样:

enter image description here

如您所见,我可以根据我选择的复选框选择复制一个链接或复制多个链接。我有两个函数,一个用于Copy按钮,另一个用于Copy selected URL,其中该函数调用第一个函数。

// multiple copy
  function copymultiplelink() {

                    var selectedFiles = $('.fileCheck:checked');
                    if(selectedFiles.length < 1 ){
                        alert("Please select at least one file.");
                        return false;
                    }
                    var filesList = [];
                    var $fileChecks = $('.fileCheck:checked');
                    $fileChecks.each(function() {
                        filesList.push($(this).val());
                    });

                    alert("You have copied " + filesList.length+ " URL");
                    copyURL(filesList);

                }


// single copy

   function copyURL(url) {
            var copyText = url;
            var el = document.createElement('textarea');
            el.value = copyText;
            el.setAttribute('readonly', '');
            el.style = {
                position: 'absolute',
                left: '-9999px'
            };
            document.body.appendChild(el);
            el.select();
            document.execCommand('copy');
            document.body.removeChild(el);
            alert("You have copied the URL");
        }

因此,调用第二个功能要容易得多,但是当我单击“复制多个”按钮时,我将收到两个警报,每个功能一个。

有一种我只能显示的方式吗?

alert("You have copied " + filesList.length+ " URL");

当我选择copy selected url按钮时?

同时,我也需要致电copyURL()复制我所有选择的URL。

3 个答案:

答案 0 :(得分:2)

希望我能很好地理解你。您不想在copymultiplelink()已显示警报时显示警报。然后,也许用标记告诉copyURL(url)显示或不显示味精来调用copyURL(url)


function copyURL(url, showMsg) {

  // your code

  if (showMsg === true) {
    alert("You have copied the URL");
  }

}

然后在copymultiplelink()内告诉它不显示消息

copyURL(filesList, false);

答案 1 :(得分:2)

这样做:

// multiple copy
function copymultiplelink() {

                var selectedFiles = $('.fileCheck:checked');
                if(selectedFiles.length < 1 ){
                    alert("Please select at least one file.");
                    return false;
                }
                var filesList = [];
                var $fileChecks = $('.fileCheck:checked');
                $fileChecks.each(function() {
                    filesList.push($(this).val());
                });

                alert("You have copied " + filesList.length+ " URL");
                copyURL(filesList, false);

            }


// single copy

function copyURL(url, isAlertNeeded=true) {
        var copyText = url;
        var el = document.createElement('textarea');
        el.value = copyText;
        el.setAttribute('readonly', '');
        el.style = {
            position: 'absolute',
            left: '-9999px'
        };
        document.body.appendChild(el);
        el.select();
        document.execCommand('copy');
        document.body.removeChild(el);
        isAlertNeeded && alert("You have copied the URL");
    }

答案 2 :(得分:1)

您可以简单地向copyUrl(url, showAlert)添加一个参数,然后copymultiplelink可以将showAlert设置为false来调用copyUrl,而您的按钮将showAlert设置为true时可以调用它。

不要忘记根据copyUrl函数中的showAlert值显示警报:

代码:

// multiple copy
  function copymultiplelink() {

                    var selectedFiles = $('.fileCheck:checked');
                    if(selectedFiles.length < 1 ){
                        alert("Please select at least one file.");
                        return false;
                    }
                    var filesList = [];
                    var $fileChecks = $('.fileCheck:checked');
                    $fileChecks.each(function() {
                        filesList.push($(this).val());
                    });

                    alert("You have copied " + filesList.length+ " URL");
                    copyURL(filesList, false); // false to not show alert

                }


// single copy

   function copyURL(url, showAlert) {
            var copyText = url;
            var el = document.createElement('textarea');
            el.value = copyText;
            el.setAttribute('readonly', '');
            el.style = {
                position: 'absolute',
                left: '-9999px'
            };
            document.body.appendChild(el);
            el.select();
            document.execCommand('copy');
            document.body.removeChild(el);
            if (showAlert) {
               alert("You have copied the URL");
            }
        }