我试图截断从我的数据库中检索到的书名,以便在条形码标签上打印。我不能从查询中截断它,因为我需要整个标题出现在同一页面上的表中。这就是我所拥有的:
$.ajax({
type: "POST",
url: "advanceProcess.php",
dataType: "json",
data: ({sourceID: $('#sourceID').val(), fromDate: $('#fromDate').val(), toDate: $('#toDate').val()}),
success: function(data){
if(data.isbn == null ){
$("#acctRecords").append('<tr><td>No Records Found</td></tr>');
}else{
//append general data
for(var x=0;x<data.isbn.length;x++)
{
$("#acctRecords").append('<tr><td id="tableSKU">'+data.tempsku[x]+
'</td><td id="tableISBN">'+data.isbn[x]+
'</td><td id="tableTitle">'+data.title[x]+
'</td><td id="tableOrderid">'+data.orderId[x]+
'</td><td id="tableQtyBought">'+data.quantity[x]+
'</td><td id="tableCost">'+data.cost[x]+
'</td><td id="tabledateCreated">'+data.dateCreated[x]+
'</td><td id="tableWeight">'+data.weight[x]+
'</td><td id="tableCheckNumber">'+data.checkNumber[x]+'</td></tr>');
}// end of for loop
//refreshes the tablesorter plugin
$("#acctRecords").trigger("update");
//Print the bar codes
sku = data.tempsku;
title = data.title[x];
//console.log(JSON.stringify(data));
title = title.substr(0,16);
var x=0;
for (x=0;x<title.length;x++)
{
first += '$("#'+indexGlobal+'").barcode("'+sku[x]+'", "codabar",{barHeight:40, fontSize:30, output:"bmp"});';
second += '<div class="wrapper"><img src="../images/temp.jpg" /><div id="'+indexGlobal+
'"></div><div class="fullSKU">      '+sku[x]+'</div><br/><div class="title">'+title[x]+
'</div></div><br/><div class="page-break"></div>';
indexGlobal++;
}
一切都很好,除了标题太长。目前我收到一个错误,指出:TypeError:title.substr不是一个函数。我在这里和谷歌做了一些研究,似乎我的代码是正确的。有什么想法吗?
编辑:以下是JSON的结果:
"title":["Understanding and Applying Medical Anthropology","The Ethical Chemist : Professionalism and Ethics in Science","Magic, Witchcraft, and Religion: A Reader in the Anthropology of Religion","Principles of Cancer Biology","AIDS and Accusation: Haiti and the Geography of Blame","In Search of Respect: Selling Crack in El Barrio (Structural Analysis in the Social Sciences)"]
编辑2:我更新了脚本以显示整个ajax调用以及返回结果的方式。
答案 0 :(得分:1)
您正在使用多个标题,以便从阵列中获取第一个:
title = data.title[0];
title = title.substr(0,16);
如果您想为每个标题构建条形码:
for(x=0; x<data.title.length; x++)
{
title = data.title[x];
title = title.substr(0,16);
// Build barcode
}
答案 1 :(得分:0)
//Your object [JSON]
var obj = {"title":["Understanding and Applying Medical Anthropology","The Ethical Chemist : Professionalism and Ethics in Science","Magic, Witchcraft, and Religion: A Reader in the Anthropology of Religion","Principles of Cancer Biology","AIDS and Accusation: Haiti and the Geography of Blame","In Search of Respect: Selling Crack in El Barrio (Structural Analysis in the Social Sciences)"]}
//Result
obj.title[0] // Understanding and Applying Medical Anthropology