weixin_33725807 2018-03-11 06:20 采纳率: 0%
浏览 18

按ID的javascript按钮值

Right now I have multiple rows from mysql printing off the following in php:

 while($row=mysql_fetch_array($result)){
echo "<input id='dd".$row["id"]."' onclick='myFunctions()' type='button' value='".$row["id"]."'></form>";}

I am able to retrieve only the most recent value of button dd.

 function myFunctions() {
 var name = document.getElementById("dd").value;

There are multiple rows though, so how can I get the value of the specific one that was clicked?

This is what the html looks like:

 <input id="dd4199" onclick="myFunctions()" value="4199" type="button">
 <input id="dd4198" onclick="myFunctions()" value="4198" type="button">
 <input id="dd4197" onclick="myFunctions()" value="4197" type="button">
 <input id="dd4196" onclick="myFunctions()" value="4196" type="button">

As you can see when it does getElementById it always finds the 4199 because it is the most recent. How can the respective click be found. Thanks!

  • 写回答

5条回答 默认 最新

  • weixin_33721427 2018-03-11 06:27
    关注

    You have to create inputs with different ids. It’s not correct to have the same id in 2 different controls.

    You can iterate and create ids dd1, dd2, etc.

    Check this: Can multiple different HTML elements have the same ID if they're different elements?

    评论

报告相同问题?

悬赏问题

  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大