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); 
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效