dqqxkq4047 2018-01-24 19:38
浏览 82
已采纳

php生成的html onclick变量不起作用

so I have this php code

$listing .= "<button onclick='updateValue(".$id.", ".$key.")'>change value</button>";

$id is an integer and works just fine, key is a reference from a for loop and the value translate well in the html code however the key value table is recognised as a variable instead of a string.

HTML: onclick="updateValue(3, table)"

I have tried evey trick I know (creating new variable with quotes, adding single quotes in the onclick and (string)$key ) to convert it as a string but nothing is working so far. any idea please ? is it even possible ?

  • 写回答

2条回答 默认 最新

  • douyan1321 2018-01-24 19:40
    关注

    You need to add quotes to tell javascript that it's a string. Since you're inside an echo you'll need to escape the quotes:

    $listing .= "<button onclick='updateValue(".$id.", \"".$key."\")'>change value</button>";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 高价求中通快递查询接口
  • ¥15 解决一个加好友限制问题 或者有好的方案
  • ¥15 关于#java#的问题,请各位专家解答!
  • ¥15 急matlab编程仿真二阶震荡系统
  • ¥20 TEC-9的数据通路实验
  • ¥15 ue5 .3之前好好的现在只要是激活关卡就会崩溃
  • ¥50 MATLAB实现圆柱体容器内球形颗粒堆积
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
  • ¥20 vitis-ai量化基于pytorch框架下的yolov5模型
  • ¥15 如何实现H5在QQ平台上的二次分享卡片效果?