将逗号php转换为javascript

Inverted comma - php to javascript

本文关键字:javascript 转换 php      更新时间:2023-09-26

考虑这行JavaScript

str += "onclick=runTest(" +  field + "," + atom[0] + ",'" + atom[1] + "'); 

在浏览器中,它呈现为:

onclick="runTest(104,213,'Orsan" Miller');

Orsan和Miller之间有一个倒逗号,尽管事实上没有任何倒逗号,但这导致了一个错误。

atom[1] = Orsan Miller

"Orsan Miller"来自PHP中的DB查询。

如何解决这个问题?

您缺少一些引号和转义。。。试试这个:

str += "onclick='"runTest(" +  field + "," + atom[0] + ",'" + atom[1] + "')'""; 

为了可读性,我更喜欢使用单引号,但这只是我自己:

str += 'onclick="runTest(' + field + ',' + atom[0] + ',''' + atom[1] + ''')"';
str += "onclick=runTest(" +  field + "," + atom[0] + ",'" + atom[1] + "')"; 

这就是你想要的,很好,只是你忘了最后的双引号。