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 黄永刚的晶体塑性子程序中输入的材料参数里的晶体取向参数是什么形式的?
  • ¥20 数学建模来解决我这个问题
  • ¥15 计算机网络ip分片偏移量计算头部是-20还是-40呀
  • ¥15 stc15f2k60s2单片机关于流水灯,时钟,定时器,矩阵键盘等方面的综合问题
  • ¥15 YOLOv8已有一个初步的检测模型,想利用这个模型对新的图片进行自动标注,生成labellmg可以识别的数据,再手动修改。如何操作?
  • ¥30 NIRfast软件使用指导
  • ¥20 matlab仿真问题,求功率谱密度
  • ¥15 求micropython modbus-RTU 从机的代码或库?
  • ¥15 django5安装失败
  • ¥15 Java与Hbase相关问题