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?

    评论

报告相同问题?