繁体   English   中英

在JS中正确使用长度

[英]proper use of length in JS

我正在尝试使用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?

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

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

这使isbn成为字符串。 每次向其中添加一个isbn时,您只是在增加字符串的长度。 "string".length将告诉您该字符串中的字符数,这就是为什么每次迭代只能得到一个字符的原因。

您需要一个数组,您可以使用[].push()方法在其中添加项。 [].length将告诉您该数组中的项目数。

var isbn = [];
...
isbn.push($(this).find('#tableISBN').html());
for (var x=0; x<isbn.length; x++) {
  isbn[x]; // one isbn
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM