簡體   English   中英

用jquery和substring截斷書名

[英]truncating the book title with jquery and substring

我正在嘗試截斷從我的數據庫中檢索出的書名以打印在條形碼標簽上。 我無法從查詢中截斷它,因為我需要整個標題才能出現在同一頁面的表格中。 這是我所擁有的:

   $.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">&nbsp &nbsp &nbsp '+sku[x]+'</div><br/><div class="title">'+title[x]+
        '</div></div><br/><div class="page-break"></div>';
        indexGlobal++;
    }           

一切都正常,除了標題太長。 當前,我收到一條錯誤消息,指出:TypeError:title.substr不是一個函數。 我已經在這里和Google上進行了一些研究,看來我的代碼是正確的。 有任何想法嗎?

編輯:這是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 = 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

}
//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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM