dsfew215211 2013-08-28 20:07
浏览 25
已采纳

onClick with参数中的字符串

Hey i'm in the middle of making an event calendar. Each event in the calendar is respresented by a button and when i click it i want to pass some variables with it. Right now it will only let me pass the $id and for some reason neither of my two string variables ($description and $title)

Code for the onClick button and the foreach loop where i get the information:

 $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 . '"/>';

    }
}

Here is the function that's supposed to create the dialog based on the variables passed

var showDialog = function(id, description, title) {
$("#dialog-modal").dialog({title:title});

};

I have tried seperating the variables with commas with no luck.

Thanks in advance.

  • 写回答

3条回答 默认 最新

  • duanbu1998 2013-08-28 20:10
    关注

    I think you probably just weren't separating with commas correctly. Try this:

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

    You may also run into issues if your values contain single quotes.

    Another option that might work better is putting each value in a data- attribute, binding the events with jQuery, and grabbing the values from the data- attributes in the handler.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题
  • ¥15 企业资源规划ERP沙盘模拟
  • ¥15 树莓派控制机械臂传输命令报错,显示摄像头不存在
  • ¥15 前端echarts坐标轴问题
  • ¥15 ad5933的I2C
  • ¥15 请问RTX4060的笔记本电脑可以训练yolov5模型吗?
  • ¥15 数学建模求思路及代码
  • ¥50 silvaco GaN HEMT有栅极场板的击穿电压仿真问题