Stop jQuery from repeating, example attached

后端 未结 1 964
不思量自难忘°
不思量自难忘° 2021-01-16 20:24

I have to attach a jQuery to an invoice. Sometimes I have to print multiple invoices as once in a batch. When this happens my exact same jQuery appears for every invoice and

相关标签:
1条回答
  • 2021-01-16 21:00

    Change your repeating template to include a check:

    <script>
    if (typeof alreadyDone === "undefined") {
       var alreadyDone = true;
    
       $(document).ready(function() {
         $('td.barcode_needed').append('<div class="bcTarget">');
    
         $('.bcTarget').each(function() {
           var $this = $(this);
           $this.barcode('G' + $this.closest('td.barcode_needed').text(),'code128');
         });    
       });
    }
    </script>
    

    Now it should run only once.

    Another thing to try, where we remove the classes as we go...

    <script>
     $('td.barcode_needed').each(function() {
           $(this).append('<div class="bcTarget">');
     });
    
     $('.bcTarget').each(function() {
       $(this).barcode('G' + $(this).closest('td.barcode_needed').text(),'code128');
       $(this).removeClass('bcTarget');       
       $(this).closest('td.barcode_needed').removeClass('barcode_needed');
     });    
    </script>
    
    0 讨论(0)
提交回复
热议问题