【问题标题】:onClick with string in parameteronClick 参数中带有字符串
【发布时间】:2013-08-29 04:16:41
【问题描述】:

嘿,我正在制作活动日历。日历中的每个事件都由一个按钮表示,当我单击它时,我想传递一些变量。现在它只会让我传递 $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});

};

我试过用逗号分隔变量,但没有成功。

提前致谢。

【问题讨论】:

  • 它们应该用逗号分隔,并注意引号
  • 试试.id.','.$description.','.$title.
  • 另外,如果值是字符串,你需要在它们周围加上引号,所以它应该是.\'$id\'.','.\'$description\'.','.\'$title\'.

标签: php javascript jquery html jquery-ui-dialog


【解决方案1】:

我认为您可能只是没有正确地用逗号分隔。试试这个:

onclick="showDialog(\''.$id.'\',\''.$description.'\',\''.$title.'\')"

如果您的值包含单引号,您也可能会遇到问题。

另一个可能效果更好的选择是将每个值放入 data- 属性中,使用 jQuery 绑定事件,并从处理程序中的 data- 属性中获取值。

【讨论】:

  • 请不要用camelCase写属性名。
【解决方案2】:

您是否尝试过引用字符串变量:

$pageContent .= '<input type="button" class="event" onClick="showDialog(\''.$id. '\',\''.$description. '\',\''.$title.'\')" value="' . $title . '"/>';

【讨论】:

  • 我忘了,你也必须用逗号分隔参数
【解决方案3】:

您当然需要使用逗号。如果您使用逗号,但它仍然无法正常工作,我会

echo $description;
echo $title;

只是为了确保它们已被填充并且您没有传递空变量。

【讨论】:

    猜你喜欢
    • 2023-02-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多