douyan3478 2019-02-21 15:51
浏览 119
已采纳

在行数据中包含特殊字符

Some of the records I'm bringing back from the database include an apostrophe.

Various records have a name like the following:

CC CHÂTEAU D'IF

Inspecting the element, the data-attribute looks like this:

data-vesselname="CC CHÂTEAU D" IF'

I need the data-attribute to look like this:

data-vesselname="CC CHÂTEAU D'IF"

Here is the ajax call:

$.ajax({
  url: 'api/searchVoyageInfo.php',
  type: 'POST',
  data: '',
  dataType: 'html',
  success: function(data, textStatus, jqXHR){
    var jsonObject = JSON.parse(data); 
    var table = $('#example1').DataTable({
      "data": jsonObject,
      "columns": [{ 
        "data": "",
        "fnCreatedCell": function (nTd, sData, oData, iRow, iCol)
        {
          $(nTd).html("<a href='#' title='Edit Account' class='modAccount'
          data-vesselname='"+oData.VESSEL_NAME+"'>Edit</a>");       
        }
       },
       { "data": "ANOTHER_COLUMN" },
       { "data": "ANOTHER_COLUMN" },
       // SEVERAL MORE COLUMNS
      ] 
});

I removed a lot of code that wasn't necessary for this question. You can see the data-vesselname attribute above. The problem seems to occur there.

I tried to do the following:

data-vesselname='"+escape(oData.VESSEL_NAME)+"'

But upon inspecting the element, I see this:

data-vesselname="CC%20CH%C2TEAU%20D%27IF"
  • 写回答

1条回答 默认 最新

  • dongli2000 2019-02-21 15:57
    关注

    You need to escape the quotes in the string you append. You can either do this manually, or you can let jQuery do it for you:

    "fnCreatedCell": function(nTd, sData, oData, iRow, iCol) {
      $('<a />', {
        'href': '#',
        'title': 'Edit Account',
        'class': 'modAccount',
        'data-vesselname': oData.VESSEL_NAME,
        'text': 'Edit'
      }).appendTo(nTd); 
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 删除虚拟显示器驱动 删除所有 Xorg 配置文件 删除显示器缓存文件 重启系统 可是依旧无法退出虚拟显示器
  • ¥15 vscode程序一直报同样的错,如何解决?
  • ¥15 关于使用unity中遇到的问题
  • ¥15 开放世界如何写线性关卡的用例(类似原神)
  • ¥15 关于并联谐振电磁感应加热
  • ¥15 this signal is connected to multiple drivers怎么解决
  • ¥60 请查询全国几个煤炭大省近十年的煤炭铁路及公路的货物周转量
  • ¥15 请帮我看看我这道c语言题到底漏了哪种情况吧!
  • ¥66 如何制作支付宝扫码跳转到发红包界面
  • ¥15 pnpm 下载element-plus