嘿,我正在制作事件日历。日历中的每个事件都由一个按钮表示,当我单击它时,我想传递一些变量。现在它只让我传递 $id 并且由于某种原因我的两个字符串变量($description 和 $title)都不传递
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) {
$id = $currentEvent['id'];
$title = $currentEvent['title'];
$description = $currentEvent['description'];
$pageContent .= '<input type="button" class="event" onClick="showDialog('.$id. $description. $title.')" value="' . $title . '"/>';
}
}
这是应该根据传递的变量创建对话框的函数
var showDialog = function(id, description, title) {
$("#dialog-modal").dialog({title:title});
};
我尝试用逗号分隔变量,但没有成功。
提前致谢。
最佳答案
我认为您可能只是没有正确地用逗号分隔。试试这个:
onclick="showDialog(\''.$id.'\',\''.$description.'\',\''.$title.'\')"
如果您的值包含单引号,您也可能会遇到问题。
另一个可能效果更好的选项是将每个值放入 data-
属性中,使用 jQuery 绑定(bind)事件,然后从 data-
属性中获取值处理程序。
关于php - onClick 参数中带有字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18497283/