Php 参数中带字符串的onClick 嘿,我正在制作一个活动日历。日历中的每个事件都由一个按钮响应,当我单击它时,我想传递一些变量。现在,它只允许我传递$id,出于某种原因,我的两个字符串变量($description和$title)都不允许传递
onClick按钮和foreach循环的代码,我从中获取信息:Php 参数中带字符串的onClick 嘿,我正在制作一个活动日历。日历中的每个事件都由一个按钮响应,当我单击它时,我想传递一些变量。现在,它只允许我传递$id,出于某种原因,我的两个字符串变量($description和$title)都不允许传递,php,javascript,jquery,html,jquery-ui-dialog,Php,Javascript,Jquery,Html,Jquery Ui Dialog,onClick按钮和foreach循环的代码,我从中获取信息: $event_day = $day_num . '.' . $month . '.' . $year; foreach ($eventArray as $currentEvent) { $result = $string->convert_timestamp_to_string($currentEvent['start_date']); if ($result === $event_day) {
$event_day = $day_num . '.' . $month . '.' . $year;
foreach ($eventArray as $currentEvent) {
$result = $string->convert_timestamp_to_string($currentEvent['start_date']);
if ($result === $event_day) {
$id = $currentEvent['id'];
$title = $currentEvent['title'];
$description = $currentEvent['description'];
$pageContent .= '<input type="button" class="event" onClick="showDialog('.$id. $description. $title.')" value="' . $title . '"/>';
}
}
})
我尝试过用逗号分隔变量,但没有成功
提前谢谢。我想你可能只是没有正确地用逗号分隔。试试这个:
onclick="showDialog(\''.$id.'\',\''.$description.'\',\''.$title.'\')"
如果值包含单引号,也可能会遇到问题
另一种可能更有效的方法是将每个值放入
data-
属性中,用jQuery绑定事件,并从处理程序中的data-
属性中获取值。您当然需要使用逗号。如果你用的是逗号,但它仍然不起作用,我会的
echo $description;
echo $title;
只是为了确保它们已填充并且您没有传递空变量。您是否尝试引用字符串变量:
$pageContent .= '<input type="button" class="event" onClick="showDialog(\''.$id. '\',\''.$description. '\',\''.$title.'\')" value="' . $title . '"/>';
$pageContent.='';
它们应该用逗号分隔,注意引号.id.'、'$description.'、'$title.
此外,如果值是字符串,则需要在其周围加引号,因此它应该是\'$id\'、'$description\'、'$title\'.
请不要在camelCase中写属性名称。我放弃了,此外,还必须用逗号分隔参数
$pageContent .= '<input type="button" class="event" onClick="showDialog(\''.$id. '\',\''.$description. '\',\''.$title.'\')" value="' . $title . '"/>';