【问题标题】:truncating the book title with jquery and substring用 jquery 和子字符串截断书名
【发布时间】:2012-08-01 17:39:54
【问题描述】:

我正在尝试截断从我的数据库中检索到的书名以打印在条形码标签上。我无法从查询中截断它,因为我需要整个标题出现在同一页面上的表中。这是我所拥有的:

   $.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 is not a function。我在这里和谷歌上做了一些研究,看来我的代码是正确的。有什么想法吗?

编辑:这是 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
  • 查询上的子字符串,只查询该行两次。有道理? Soo.... SELECT columnA title, substr(columnA,0,16) sku FROM DUAL
  • 快速找出title的数据类型的方法是使用alert(typeof title);
  • 我按照@cilosis 的建议做了并得到了反对。这是否意味着我需要做 title.object.substring(0,16)?
  • 没有。您不能对对象执行substr()。仅在字符串上。 title的结构是什么?

标签: javascript jquery substring


【解决方案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

}

【讨论】:

  • 我以为我不能在对象上使用 substr。
  • 你不能。您将在字符串上使用它。 data.title[0] 等于“理解和应用医学人类学”。增加数组索引,转到数组中的下一个标题。
  • 在对其余代码稍作修改后,它可以完美运行。谢谢
  • 没问题,吉姆。很高兴能提供帮助。
【解决方案2】:
//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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-03-12
    • 1970-01-01
    • 1970-01-01
    • 2014-11-03
    • 2014-09-14
    • 2015-12-14
    相关资源
    最近更新 更多