开发者

Stop jQuery from repeating, example attached

开发者 https://www.devze.com 2023-03-12 04:53 出处:网络
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 it runs each time creating

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 it runs each time creating extra elements I do not need. Is there a way to have a jQuery that appears more then once only run once when it is the last time it appears in the code? Thanks for any help.

Example is below. I have previously posted this and everyone asked to see how I was doing it. I was not sure how to add the code to the post as it said it was too long. Thanks for all your help. You guys are awesome.

<table width=180 border=0 cellpadding=0 cellspacing=0>
                <tr> 
                  <td class="barcode_needed">10133</td>
                </tr>

<!--This section of code is attached to every order so it repeats when each order prints, (this is a simplified version of the code, I need to make it only run the last time it shows up-->                    
<link rel="stylesheet" type="text/css" href="http://barcode-coder.com/css/style.css?ft=1298939919" />
<script type="text/javascript" src="http://barcode-coder.com/js/jquery-1.3.2.min.js"></script>
<script type="text/javascript" src="http://barcode-coder.com/js/jquery-ui-1.7.custom.min.js"></script>
<script type="text/javascript" src="http://barcode-coder.com/js/jquery-barcode-last.min.js"></script>


<script>
$('td.barcode_needed').append('<div class="bcTarget">');

$('.bcTarget').each(function() {
var $this = $(this);
$this.barcode(
    'G' + $this.closest('td.barcode_needed').text(),
    'code128'
);
});


</script>
<!--End Repeating Code-->
                <tr> 
                  <td class="barcode_needed">20133</td>
                </tr>


<link rel="stylesheet" type="text/css" href="http://barcode-coder.com/css/style.css?ft=1298939919" />
<script type="text/javascript" src="http://barcode-coder.com/js/jquery-1.3.2.min.js"></script>
<script type="text/javascript" src="http://barcode-coder.com/js/jquery-ui-1.7.custom.min.js"></script>
<script type="text/javascript" src="http://barcode-coder.com/js/jquery-barcode-last.min.js"></script>


<script>
$('td.barcode_needed').append('<div class="bcTarget">');

$('.bcTarget').each(function() {
var $this = $(this);
$this.barcode(
    'G' + $this.closest('td.barcode_needed').text(),
    'code128'
);
});


</script>

                <tr> 
                  <td class="barcode_needed">30133</td>
                </tr>


<link rel="stylesheet" type="text/css" href="http://barcode-coder.com/css/style.css?ft=1298939919" />
<script type="text/javascript" src="http://barcode-coder.com/js/jquery-1.3.2.min.js"></script>
<script type="text/javascript" src="http://barcode-coder.com/js/jquery-ui-1.7.custom.min.js"></script>
<script type="text/javascript" src="http://barcode-coder.com/js/jquery-barcode-last.min.js"></script>


<script>
$('td.barcode_needed').append('<div class="bcTarget">');

$('.bcTarget').each(function() {
var $this = $(this);
$this.barcode(
    'G' + $this.closest('td.barcode_needed').text(),
    'code128'
);
})开发者_StackOverflow;


</script>

</table>


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

精彩评论

暂无评论...
验证码 换一张
取 消