dongshao1156 2013-03-08 19:18
浏览 87
已采纳

jQuery字符串未定义

I'm sending data to function by onclick event but I can't get string value I just getting integer value, it say that 'value' is not defined. what is the problem.

My code is:

<a href="javascript:void(0)" 
   onclick="begin(<?php echo $data['user_id'];?>,
   <?php echo $data['name'];?>);">       

This is my function:

function begin(id,name)
{
    alert(id);
    alert(name);
}

I'm not getting name value, if I pass hard-code string then its also not getting here only integer are accessible.

  • 写回答

2条回答 默认 最新

  • dongtao6842 2013-03-08 19:28
    关注

    As Matt says, without quotes it won't be recognised.

    That said, I don't think his answer is correct. I would prefer this code: (whitespace added for legibility)

    <a href="javascript:void(0);" onclick="begin(
        <?php echo htmlspecialchars(json_encode($data['user_id'])); ?>,
        <?php echo htmlspecialchars(json_encode($data['name'])); ?>
    );">
    

    json_encode (docs) is good for passing any PHP variable (except Resources) into JavaScript. In this case, it will add quotes around the string, and escape characters as needed with backslashes. Since it's going in an attribute, you need htmlspecialchars to convert symbols to be safely insertable.

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

报告相同问题?

悬赏问题

  • ¥15 Jenkins+k8s部署slave节点offline
  • ¥15 微信小游戏反编译后,出现找不到分包的情况
  • ¥15 如何实现从tello无人机上获取实时传输的视频流,然后将获取的视频通过yolov5进行检测
  • ¥15 WPF使用Canvas绘制矢量图问题
  • ¥15 用三极管设计一个单管共射放大电路
  • ¥15 孟德尔随机化r语言运行问题
  • ¥15 pyinstaller编译的时候出现No module named 'imp'
  • ¥15 nirs_kit中打码怎么看(打码文件是csv格式)
  • ¥15 怎么把多于硬盘空间放到根目录下
  • ¥15 Matlab问题解答有两个问题