1

我正在尝试使用 JS(该表使用 JQ Tablesorter)和条形码 jquery 从表中打印出标签(条形码)。我的问题是我需要遍历所有 isbn 并且每行显示一个数字。这是我的代码:

    $("#barcode").live('click', function(){
var title="";
var isbn="";
var first = "";
var second = "";
var indexGlobal = 0;

$('#acctRecords tbody tr').each(function()
{
    isbn += $(this).find('#tableISBN').html();
    title += $(this).find('#tableTitle').html();

    });  //end of acctRecords tbody function

//Print the bar codes

    var x=0;
    for (x=0;x<isbn.length;x++)
        {


        first += '$("#'+indexGlobal+'").barcode("'+isbn[x]+'", "codabar",{barHeight:40, fontSize:30, output:"bmp"});';
        second += '<div class="wrapper"><div id="'+indexGlobal+'"></div><div class="fullSKU">&nbsp &nbsp &nbsp '+isbn[x]+
        '</div><br/><div class="title">'+title[x]+'</div></div><br/><br/>';
        indexGlobal++;

        }
var barcode =  window.open('','BarcodeWindow','width=400');
        var html = '<html><head><title>Barcode</title><style type="text/css">'+
        '.page-break{display:block; page-break-before:always; }'+
        'body{width: 8.25in;-moz-column-count:2; -webkit-column-count:2;column-count:2;}'+
        '.wrapper{height: 2.5in;margin-left:10px;margin-top:5px;margin-right:5px;}'+
        '.fullSKU{float: left;}'+
        '.shortSKU{float: right;font-size:25px;font-weight:bold;}'+
        '.title{float: left;}'+
        '</style><script type="text/javascript"src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.js"></script><script type="text/javascript" src="../barcode/jquery-barcode.js"></script><script>$(document).ready(function() {'+first+'window.print();window.close();});</script></head><body>'+second+'</body></html>';
        barcode.document.open();
        barcode.document.write(html);
        barcode.document.close();

}); // end of click function

我很确定问题出在以下几行:

var x=0;
for (x=0;x<isbn.length;x++)

例如,如果 isbn 是 9780596515898,我在第一行得到 9,在第二行得到 7,在第三行得到 8,等等。我如何让它在一行上打印出整个 isbn?

4

1 回答 1

5

不,那两条线很好。但另一方面,这两个...

var isbn="";
...
isbn += $(this).find('#tableISBN').html();

这构成isbn了一个字符串。每次向其中添加 isbn 时,您只是使字符串变长。 "string".length将告诉您该字符串中的字符数,这就是每次迭代获得一个字符的原因。

You want an array instead, which you append items to with the [].push() method. [].length will tell you the number of items in that array.

var isbn = [];
...
isbn.push($(this).find('#tableISBN').html());
for (var x=0; x<isbn.length; x++) {
  isbn[x]; // one isbn
}
于 2012-08-08T17:17:47.220 回答