dsqtl335227 2014-10-11 13:16
浏览 117
已采纳

JAVASCRIPT:传递的参数是“未定义”

I'm creating a page that should read data from the database, display it as a table, and allow row-wise editing of the entries (using AJAX calls). Here are brief code snippets where the problem comes -

Javascript:

function editParam(bname, button) {
 alert(bname); //this part doesn't happen, the error on console comes up before it
 tr = button.parentNode;
 while (tr && tr.nodeName.toUpperCase() !== "TR" && tr.nodeName.toUpperCase() !== "BODY") {
    tr = tr.parentNode;
 }
 if (!tr || tr.nodeName.toUpperCase() !== "TR") {
    return; 
 }
 //some more code
}

PHP:

//get $row_users['Name']
while ($row_users = mysqli_fetch_array($results)) {
 echo "<form method='POST'> //some <tr><td></td></tr>
 <button onclick='editParam(".$row_users['Name'].", this)'>Edit</button>
 </form>";
}

My data in the column Name is like "B1", "C2", etc.

Error onclick of button : B1 is not defined.

While running the code for other text or numbers, it worked fine. So I understand that my problem has to do with the datatype. However, I do not know how resolve the same.

Any suggestions would be much appreciated, thanks!

  • 写回答

1条回答 默认 最新

  • dongtaijiao7140 2014-10-11 13:27
    关注

    You need to add quotes around $row_users['Name'] since otherwise it is interpreted as a JS variable.

    "<button onclick='editParam(\"" . $row_users['Name'] . "\", this)'>Edit</button>";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题