【问题标题】:Stop jQuery from repeating, example attached停止 jQuery 重复,附上示例
【发布时间】:2017-02-23 16:24:36
【问题描述】:

我必须在发票上附加一个 jQuery。有时我必须批量打印多张发票。发生这种情况时,每张发票都会出现我完全相同的 jQuery,并且每次创建我不需要的额外元素时都会运行它。有没有办法让一个出现不止一次的 jQuery 只在它最后一次出现在代码中时运行一次?感谢您的帮助。

示例如下。我以前发布过这个,每个人都要求看看我是怎么做的。我不确定如何将代码添加到帖子中,因为它说它太长了。感谢你的帮助。你们太棒了。

<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'
);
});


</script>

</table>

【问题讨论】:

    标签: jquery repeat


    【解决方案1】:

    更改您的重复模板以包括检查:

    <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>
    

    现在它应该只运行一次。

    另一件要尝试的事情,我们删除类的地方......

    <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>
    

    【讨论】:

    • 这就是问题所在,我不能只添加一次。无论我输入什么代码,每一个批处理的订单都会重复。代码必须附加到单个订单页面。
    • 当我运行它时,不再打印条码了。我已经剪切并粘贴了代码。有什么想法吗?
    • @user764728 我又更新了,请试一试,但是..问题..也许这需要在所有内容加载完毕后运行?
    • 它仍然失败。它不允许它们中的任何一个运行。是的,它确实需要在最后运行。我想要的只是要运行的 jquery 的最后一个实例
    • $(document).ready(function() {,如果我删除这部分,那么条形码会再次出现,但它们会出现多次
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-06-13
    • 1970-01-01
    • 2016-05-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多