2011-06-13 72 views
2

可能重复:
Stop jquery from repeating, example attached有jQuery的只运行一次

我附上查询到我的订单生成条形码。代码必须分别附加到每个订单上。当我们打印我们的订单时,他们会分批打印,代码会多次出现。它在每个实例上运行并生成条码的多个副本。我试图阻止它,但似乎无法得到它的工作。有人可以发送一个建议。我知道只有一次是理想的,但不能从我正在使用的网站中获得。

<script> 
$('font[size="3"] b:not(.dont_run)').addClass('barcode_needed'); 

    $('b.barcode_needed').each(function() { 
    $(this).append('  <div class="bcTarget">'); 
    }); 

    $('.bcTarget').each(function() { 
    $(this).barcode('G' + $(this).closest('b.barcode_needed').text(),'code128'); 
    $(this).removeClass('bcTarget');  
    $(this).closest('b.barcode_needed').removeClass('barcode_needed').addClass('dont_run'); 
    });  
</script> 

下面是整个代码:

<html> 
<head> 
<link href="/v/vspfiles/templates/ZuluLAX/css/Colors.css" rel="stylesheet" type="text/css"> 
<link href="/v/vspfiles/templates/ZuluLAX/css/Content_Area.css" rel="stylesheet" type="text/css"> 

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> 

</head> 
<body bgcolor="#FFFFFF" text="#000000" leftmargin="0" topmargin="0" marginwidth="0" marginheight="0" onLoad=""> 

<style type=text/css> 
BODY { 
FONT: 11px Verdana; COLOR: #000000; letter-spacing: -0.1em; 
} 
TABLE { 
FONT: 11px Verdana; COLOR: #000000; letter-spacing: -0.1em; 
} 
TD { 
FONT: 11px Verdana; COLOR: #000000; letter-spacing: -0.1em; 
} 
.pos_receipt td { 
font-family: "Courier New", Courier, monospace; 
} 
span.WarehouseLocation 
{ 
width:50px; 
border-bottom-color: black; 
border-bottom-style: dashed; 
border-bottom-width: 1px; 
} 
</style> 

<table cellpadding=0 cellspacing=0 width="100%" align="center" style="page-break-after:always;"> 
    <tr> 
    <td align="center"> 
     <table cellpadding=0 cellspacing=0 width=600> 

     <tr> 
      <td width="100%"> 
      <table width="600" border="0" cellspacing="0" cellpadding="0"> 
       <tr> 
       <td width="400"> 

       <img src="v/vspfiles/templates/ZuluLAX/images/company/logo.jpg"> 
       <div id="div_articleid_86"> 
</div> 

       </td> 
       <td width="200" align="right"> 
        <table width=180 border=0 cellpadding=0 cellspacing=0> 


        <tr> 
         <td width=90 align=left colspan="2">  <b>  <font style="font-size:18px;font-weight:bold;">PACKING SLIP  </font>  </b>  </td> 
        </tr> 

        <tr> 
         <td width=90 align=left>  <b>Date:  </b>  </td> 
         <td width=90>  <b>Order#:  </b>  </td> 
        </tr> 
        <tr> 
         <td width=75 align=left> 06/09/2011  </td> 

         <td>  <font size=3>  <b>10196  </b>  </font>  </td> 
        </tr> 
        </table> 
       </td> 
       </tr> 
      </table> 
      </td> 
     </tr> 

     <tr> 
      <td width="100%">  <img src='/clear1x1.gif' width='1' height='1' border=0>  </td> 
     </tr> 
     </table> 


<script type="text/javascript" src="http://zululax.com/v/vspfiles/templates/ZuluLAX/js/jquery.js">  </script> 
    <script type="text/javascript" src="http://zululax.com/v/vspfiles/templates/ZuluLAX/js/jquery-barcode-2.0.2.min.js">  </script> 


<script> 
$('font[size="3"] b:not(.dont_run)').addClass('barcode_needed'); 

    $('b.barcode_needed').each(function() { 
    $(this).append('  <div class="bcTarget">'); 
    }); 

    $('.bcTarget').each(function() { 
    $(this).barcode('G' + $(this).closest('b.barcode_needed').text(),'code128'); 
    $(this).removeClass('bcTarget');  
    $(this).closest('b.barcode_needed').removeClass('barcode_needed').addClass('dont_run'); 
    });  
</script> 

</div> 

    </td> 
    </tr> 
</table> 

<table cellpadding=0 cellspacing=0 width="100%" align="center" style="page-break-after:always;"> 
    <tr> 
    <td align="center"> 
     <table cellpadding=0 cellspacing=0 width=600> 
     <tr> 
      <td width="100%"> 
      <table width="600" border="0" cellspacing="0" cellpadding="0"> 
       <tr> 
       <td width="400"> 

       <img src="v/vspfiles/templates/ZuluLAX/images/company/logo.jpg"> 
       <div id="div_articleid_86"> 

</div> 

       </td> 
       <td width="200" align="right"> 
        <table width=180 border=0 cellpadding=0 cellspacing=0> 

        <tr> 
         <td width=90 align=left colspan="2">  <b>  <font style="font-size:18px;font-weight:bold;">PACKING SLIP  </font>  </b>  </td> 
        </tr> 

        <tr> 
         <td width=90 align=left>  <b>Date:  </b>  </td> 

         <td width=90>  <b>Order#:  </b>  </td> 
        </tr> 
        <tr> 
         <td width=75 align=left> 06/09/2011  </td> 
         <td>  <font size=3>  <b>10197  </b>  </font>  </td> 
        </tr> 
        </table> 

       </td> 
       </tr> 
      </table> 
      </td> 
     </tr> 
     <tr> 
      <td width="100%">  <img src='/clear1x1.gif' width='1' height='1' border=0>  </td> 
     </tr> 
     </table> 



<script type="text/javascript" src="http://zululax.com/v/vspfiles/templates/ZuluLAX/js/jquery.js">  </script> 
    <script type="text/javascript" src="http://zululax.com/v/vspfiles/templates/ZuluLAX/js/jquery-barcode-2.0.2.min.js">  </script> 


<script> 
$('font[size="3"] b:not(.dont_run)').addClass('barcode_needed'); 

    $('b.barcode_needed').each(function() { 
    $(this).append('  <div class="bcTarget">'); 
    }); 

    $('.bcTarget').each(function() { 
    $(this).barcode('G' + $(this).closest('b.barcode_needed').text(),'code128'); 
    $(this).removeClass('bcTarget');  
    $(this).closest('b.barcode_needed').removeClass('barcode_needed').addClass('dont_run'); 
    });  
</script> 
</div> 

    </td> 
    </tr> 
</table> 

<table cellpadding=0 cellspacing=0 width="100%" align="center" style="page-break-after:always;"> 
    <tr> 
    <td align="center"> 
     <table cellpadding=0 cellspacing=0 width=600> 
     <tr> 
      <td width="100%"> 
      <table width="600" border="0" cellspacing="0" cellpadding="0"> 
       <tr> 
       <td width="400"> 

       <img src="v/vspfiles/templates/ZuluLAX/images/company/logo.jpg"> 
       <div id="div_articleid_86"> 

</div> 

       </td> 
       <td width="200" align="right"> 
        <table width=180 border=0 cellpadding=0 cellspacing=0> 

        <tr> 
         <td width=90 align=left colspan="2">  <b>  <font style="font-size:18px;font-weight:bold;">PACKING SLIP  </font>  </b>  </td> 
        </tr> 

        <tr> 
         <td width=90 align=left>  <b>Date:  </b>  </td> 

         <td width=90>  <b>Order#:  </b>  </td> 
        </tr> 
        <tr> 
         <td width=75 align=left> 06/09/2011  </td> 
         <td>  <font size=3>  <b>10198  </b>  </font>  </td> 
        </tr> 
        </table> 

       </td> 
       </tr> 
      </table> 
      </td> 
     </tr> 
     <tr> 
      <td width="100%">  <img src='/clear1x1.gif' width='1' height='1' border=0>  </td> 
     </tr> 
     </table> 



<script type="text/javascript" src="http://zululax.com/v/vspfiles/templates/ZuluLAX/js/jquery.js">  </script> 
    <script type="text/javascript" src="http://zululax.com/v/vspfiles/templates/ZuluLAX/js/jquery-barcode-2.0.2.min.js">  </script> 


<script> 
$('font[size="3"] b:not(.dont_run)').addClass('barcode_needed'); 

    $('b.barcode_needed').each(function() { 
    $(this).append('  <div class="bcTarget">'); 
    }); 

    $('.bcTarget').each(function() { 
    $(this).barcode('G' + $(this).closest('b.barcode_needed').text(),'code128'); 
    $(this).removeClass('bcTarget');  
    $(this).closest('b.barcode_needed').removeClass('barcode_needed').addClass('dont_run'); 
    });  
</script> 
</div> 

    </td> 
    </tr> 
</table> 

</body> 
</html> 
+0

你的问题的区别在哪里[停止jquery重复,示例附加](http://stackoverflow.com/questions/6307028/stop-jquery-from-repeating-example-attached)? – 2011-07-02 13:04:53

回答

1

最近有同样的问题。解决方案是检查元素或类:是否存在或不存在?例如,我看到.bcTarget被附加到.barcode_needed。 所以,你可以检查这样的:

$('b.barcode_needed').each(function() { 
    // if .bcTarget does not exist within current .barcode_needed, let's append 
    if (!$(this).contents().is('.bcTarget')) 
    $(this).append('<div class="bcTarget"></div>'); 
}); 

希望,你已经抓住了这个想法。

3

我假设问题是由相乘的脚本代码引起的。如果是这样,那么:

最简单的办法就是把你的代码document.redy

$(document).ready(function(){ 
    //your script code 
}); 

并将其放置在一个外部文件,该文件将在页面加载:

<script type="javascript/text" src="Source of the file with your document.ready function" /> 
+0

如果我打电话给我,我会遇到同样的问题。它每次脚本运行时都会运行一个条形码。我需要让它只运行一次。 – user764728 2011-06-13 17:53:43

+0

你可以在脚本上停下来说出它是否再次出现不运行? – user764728 2011-06-13 18:00:37

+0

也许你把<脚本类型= ...部分放在那里你有你的脚本(它会适合你的场景..)?如果是这样,这是不正确的..你应该把它放在主文件只有一次 - 最好在部分。 – ub1k 2011-06-13 18:29:56