我的 php 函数以某种方式创建了一个调用 javascript 函数的按钮,但它没有将 php 变量作为字符串传递。
function addTableEntry($id, $name)
{
echo('<tr>
<td>'.$id.'</td>
<td>'.$name.'</td>
<td><a href="#groups" onClick="activateGroupSettingsOverlay('.$id.','.$name.')">Manage group</a>
</tr>');
}
addTableEntry(1,"livingroom");
每当单击函数 activateGroupSettingsOverlay()
时,总是会调用 (1, livingroom) 并且出现错误“livingroom is undefined”。
如何将 $name 作为字符串传递?我试着在它周围加上引号(像这样:'.$id.',"'.$name.'"
,但那没有用。
最佳答案
如果你想让引号持续存在,你必须在字符串中“转义”引号:
echo '..... onClick="activateGroupSettingsOverlay('.$id.',\''.$name.'\')"....'
重要的是(单)引号前的反斜杠。
关于javascript - 将 PHP 变量作为字符串传递给 javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37482386/