gpt4 book ai didi

javascript - 如何在 onclick 函数中添加单引号?

转载 作者:行者123 更新时间:2023-12-02 23:05:45 27 4
gpt4 key购买 nike

我的代码在这里printNextPeriod(新日期(2019-01-01),新日期(2020-01-01),26);需要在日期之间添加单引号

期待像

printNextPeriod(new Date('2019-01-01'), new Date('2020-01-01'),26);

我需要在哪里修复以下代码中的此错误?

 if (data == 'pestcontrol') {
return ('<button onclick="printNextPeriod(new Date('+row["first_job_dt"]+'), new Date('+row["end_date"]+'),'+row["day_bw_job"]+');" data-jobdays="'+ row["day_bw_job"] +'" data-fjd="'+ row["first_job_dt"] +'" data-actiendcontr="'+ row["end_date"] +'" class="btn btn-success btn-sm actpest" data-target="#viewactipestmodal" data-keyboard="false" data-backdrop="static" data-toggle="modal">View Schedule-P</button>');
}

数据表中的这些代码

{
"data" : "pest_or_clean",
"bSortable": false,
render: function (data, type, row, rowData) {
//console.log(data);
if (data == 'pestcontrol') {
return ('<button onclick="printNextPeriod(new Date('+row["first_job_dt"]+'), new Date('+row["end_date"]+'),'+row["day_bw_job"]+');" data-jobdays="'+ row["day_bw_job"] +'" data-fjd="'+ row["first_job_dt"] +'" data-actiendcontr="'+ row["end_date"] +'" class="btn btn-success btn-sm actpest" data-target="#viewactipestmodal" data-keyboard="false" data-backdrop="static" data-toggle="modal">View Schedule-P</button>');
}
else {
return ('<button data-jobdays="'+ row["day_bw_job"] +'" data-fjd="'+ row["first_job_dt"] +'" data-actiendcontr="'+ row["end_date"] +'" class="btn btn-success btn-sm actclean" data-target="#viewacticleanmodal" data-keyboard="false" data-backdrop="static" data-toggle="modal">View Schedule-C</button>');
}
}
}

最佳答案

使用转义引号。

if (data == 'pestcontrol') {
return ('<button onclick="printNextPeriod(new Date(\''+row["first_job_dt"]+'\'), new Date(\''+row["end_date"]+'\'),'+row["day_bw_job"]+');" data-jobdays="'+ row["day_bw_job"] +'" data-fjd="'+ row["first_job_dt"] +'" data-actiendcontr="'+ row["end_date"] +'" class="btn btn-success btn-sm actpest" data-target="#viewactipestmodal" data-keyboard="false" data-backdrop="static" data-toggle="modal">View Schedule-P</button>');
}

如果使用 ES6 模板文字,那就更简单了。此外,当对象中的属性名称是文字时,您可以编写 variable.prop 而不是更详细的 variable["prop"]

if (data == 'pestcontrol') {
return (`<button onclick="printNextPeriod(new Date('${row.first_job_dt}'), new Date('${row.end_date}'),${row.day_bw_job}');" data-jobdays="${row.day_bw_job}" data-fjd="${row.first_job_dt}" data-actiendcontr="${row.end_date}" class="btn btn-success btn-sm actpest" data-target="#viewactipestmodal" data-keyboard="false" data-backdrop="static" data-toggle="modal">View Schedule-P</button>`);
}

关于javascript - 如何在 onclick 函数中添加单引号?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57600614/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com