简体   繁体   English

未捕获的类型错误:$(...)[0].select 不是 function

[英]Uncaught TypeError: $(...)[0].select is not a function

I'm using SweetAlert to show an alert after I copy some text info.复制一些文本信息后,我正在使用 SweetAlert 显示警报。 I loop through the following code 5 times (because I have 5 different entries of text), increasing $num by 1 each time.我遍历以下代码 5 次(因为我有 5 个不同的文本条目),每次将 $num 增加 1。

       <button  class="js-textareacopybtn copy-btn btn-info" data-id="<?php echo $num  ?>">COPY</button>

Here is the script这是脚本

 <script>
    $('.js-textareacopybtn').on('click', function(event) {
    var copyTextarea = $(this).data('id');
    $('#' + copyTextarea)[0].select();
    var msg=$('#' + copyTextarea)[0].value;
    try {
      var successful = document.execCommand('copy');
         Swal.fire({
         title: 'Copied!',
         text: 'Ready to paste and send',
         icon: 'success',
         showConfirmButton: false,
         timer: 1800
      })

      } catch (err) {
        alert('Unable to copy');
      }
    });

  </script>

This runs perfectly fine for the first 4 loops...the $num is correct and so is copyTextarea.这对于前 4 个循环运行得非常好……$num 是正确的,copyTextarea 也是正确的。 I can我可以

But on the 5th run I get this error (even though $num is correct (it equals 5) and I validate that coptyTextarea is correct (also equals 5))但是在第 5 次运行时,我得到了这个错误(即使 $num 是正确的(它等于 5)并且我验证 coptyTextarea 是正确的(也等于 5))

    Uncaught TypeError: $(...)[0].select is not a function
    

So the error occurs for the line所以该行发生错误

   $('#' + copyTextarea)[0].select();

I have no idea what could be going wrong我不知道出了什么问题

ok...the problem was that there was another id with the same value for just that one id..oh well, would never have found it since it was a quirk.好的...问题是,只有一个 id 有另一个具有相同值的 id..哦,好吧,因为这是一个怪癖,所以永远不会找到它。

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

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