我試圖截斷從我的數據庫中檢索到的書籍標題打印在條形碼標籤上。我無法從查詢中截斷它,因爲我需要將整個標題顯示在同一頁面上的表格中。這是我有:截斷書名與jQuery和子字符串
$.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調用,並將結果返回方式。
你確定'title'的類型是'string'嗎? – Matt 2012-08-01 17:44:47
查詢的子串,只查詢兩次。合理? Soo .... SELECT columnA title,substr(columnA,0,16)sku FROM DUAL – 2012-08-01 17:45:52
查找'title'的數據類型的快速方法是使用'alert(typeof title);' – 2012-08-01 17:46:06