You need to enable JavaScript to run this app. jQuery DataTable列渲染出现多余双引号引发JS语法错误问题
优惠活动
大模型
产品
解决方案
定价
更多

jQuery DataTable列渲染出现多余双引号引发JS语法错误问题

问题背景

以下为两段jQuery DataTable初始化配置代码:
第一段为customerTable配置:

var sedatatable = $('#customerTable').DataTable({
    "rowHeight": '5px',
    "searchHighlight": true,
    "autoWidth": false,
    "stateSave": true,
    "pageLength": 25,
    "ajax": {
        "url":"@Url.Action("GetList", "customers")",
        "type": "GET",
        "datatype": "json" ,
        //"cache": true
    },
    "columns": [
        { "data": "id", "width": "50%" , className: "dt-body-center text-center"},
        {
            "data": "name", "width": "55%", 
            "render": function (data, type, full) {    
                return '<a style="color:#337AB7"  onclick=EditSequencesName(' + data + ')>' + data + '</a>';
            }
        }
    ],
});

第二段为sequencesTable配置:

var sedatatable, rowdata;
sedatatable = $('#sequencesTable').DataTable({
    "rowHeight": '5px',
    "searchHighlight": true,
    "autoWidth": false,
    "stateSave": true,
    "pageLength": 25,
    "ajax": {
        "url":"@Url.Action("GetList", "Customers")",
        "type": "GET",
        "datatype": "json" ,
        //"cache": true
    },
    "columns": [
        {
            "data": "Active", "searchable": "false",
            "render": function (data, type, row) {
                if (type === 'display') {
                    return '<input type="checkbox"   class="editor-active"/>';
                }
                return data;
            },
            "width":"5%",
            className: "dt-body-center text-center"
        },
        {
            "data": "ID", "width": "8%", className: "dt-body-center text-center"
        },
        {
            "data": "Name", "width": "55%",
            "render": function (data,type, row, full, meta) {
                return '<a style="color:#337AB7"  onclick="EditSequencesName(\'' + $.trim(data) + '\')">' + data + '</a>'; 
            }
        },
    ],
    fixedColumns:   {
        heightMatch: 'none'
    },
    rowCallback: function (row, data) {
        $('input.editor-active', row).prop('checked', data.Active == 1);
    }
});

异常表现

通过开发者工具查看customerTable第二列实际渲染结果为:

<a style="color:#337AB7" onclick="EditcustomerName(Test1" )>Test1</a>
  • 文本Test1后出现多余双引号
  • 控制台抛出错误:(index):1 Uncaught SyntaxError: missing ) after argument list
问题原因

问题出在第一段customerTable的name列render逻辑:

  1. HTML属性书写不规范:onclick属性值没有用引号包裹,浏览器解析HTML时自动判断属性边界出现错位,错误地在参数Test1后补了属性闭合双引号,导致括号位置错乱。
  2. JS参数语法错误:传入点击事件函数的data是字符串类型,但拼接时没有给参数加引号,即使属性解析正常,浏览器也会把Test1识别为未定义的JS变量,而非字符串参数,同样会触发报错。

对比第二段sequencesTable的render写法,已经正确做了属性包裹和参数转义,所以不会出现该问题。

修复方案

快速修复(对齐正确写法)

修改customerTable的name列render返回值,和第二段保持一致:给onclick属性整体加双引号包裹,给传入的字符串参数加转义单引号,同时提前用$.trim去除参数首尾空格避免异常:

"render": function (data, type, full) {    
    const safeName = $.trim(data);
    return '<a style="color:#337AB7" onclick="EditSequencesName(\'' + safeName + '\')">' + safeName + '</a>';
}

更稳妥的优化方案(避免转义问题)

内联事件拼接很容易因为特殊字符(比如参数里带单引号、双引号)出现解析错误,推荐改用事件委托的方式绑定点击事件,从根源上避免转义冲突:

  1. 修改render返回值,去掉内联onclick,新增class和data属性存储参数:
"render": function (data, type, full) {    
    const safeName = $.trim(data);
    return '<a style="color:#337AB7" class="edit-name-btn" data-name="'+ safeName +'">' + safeName + '</a>';
}
  1. 在DataTable初始化代码外,统一给表格绑定委托事件:
$('#customerTable').on('click', '.edit-name-btn', function() {
    const currentName = $(this).data('name');
    EditSequencesName(currentName);
});

内容的提问来源于stack exchange,提问作者anu

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.29 14:03:28