[英]javascript - get clicked button value when all buttons are named the same
I have a situation where I have lots of buttons in a table on my web page.我的 web 页面上的表格中有很多按钮。
Each button has the same name, but a different value set.每个按钮具有相同的名称,但设置不同的值。
When I want to extract the button value, wouldn't using the below mean I am not getting the unique button value - the one that was actually clicked?当我想提取按钮值时,不使用以下是否意味着我没有获得唯一的按钮值 - 实际点击的那个?
How can I get around that?我怎样才能解决这个问题?
Thanks!!谢谢!!
document.getElementById("myBtn").value;
I think some of the comments / answers may be a little confusing.我认为一些评论/答案可能有点令人困惑。 It might help to bind the same handler to each button and have it handle accordingly.
将相同的处理程序绑定到每个按钮并对其进行相应处理可能会有所帮助。
As @chr stated, try using something which will allow you to query all the buttons you want to attach the same click event to.正如@chr 所说,尝试使用允许您查询要附加相同点击事件的所有按钮的东西。
document.querySelectorAll(".myBtn");
Is one approach, however I suggest looking at how you can play with that query selector if you want to be a little more generic and get all buttons within your table.是一种方法,但是如果您想要更通用一点并获取表格中的所有按钮,我建议您查看如何使用该查询选择器。
Next, I suggest using a common click handler for each.接下来,我建议为每个使用一个通用的点击处理程序。 In a javascript event, you are passed an event object as a parameter which will allow you to get information on the DOM element which initiated the event.
在 javascript 事件中,您会收到一个事件 object 作为参数,它允许您获取有关启动事件的 DOM 元素的信息。
function myClickHandler(event) {
var value = event.target.value;
// Do stuff
}
document.querySelectorAll(".myBtn").forEach(btn => {
btn.addEventListener("click", myClickHandler);
});
First of all an id
should be a unique identifier on the document.首先,
id
应该是文档上的唯一标识符。
If it is not unique you should use a class like: document.querySelectorAll(".myBtn");
如果它不是唯一的,则应使用 class ,例如:
document.querySelectorAll(".myBtn");
. .
Yes it might not be practical to id
each button in a table.是的,
id
表格中的每个按钮可能不切实际。 Lets imagine you have a buttons table like假设您有一个按钮表,例如
function getButton(el){ console.log(el.target.value); }
<table onclick="getButton(event)"> <tr> <td><button name="button" value="1-1">Click</button></td> <td><button name="button" value="1-2">Click</button></td> <td><button name="button" value="1-3">Click</button></td> </tr > <tr> <td><button name="button" value="2-1">Click</button></td> <td><button name="button" value="2-2">Click</button></td> <td><button name="button" value="2-3">Click</button></td> </tr > <tr> <td><button name="button" value="3-1">Click</button></td> <td><button name="button" value="3-2">Click</button></td> <td><button name="button" value="3-3">Click</button></td> </tr > </table>
So when you click the button, you get the value.因此,当您单击按钮时,您将获得值。 However this might not be what you are asking.
但是,这可能不是您要问的。 If you would like to select a particular button in the table by JS dynamically, then you may easily do so by indexing the buttons through CSS selector logic as follows;
如果您想通过 JS 动态地 select 表格中的特定按钮,那么您可以通过 CSS 选择器逻辑索引按钮来轻松实现,如下所示;
function getButtonValue(r,c){ var el = document.querySelector(`table tr:nth-child(${r}) td:nth-child(${c}) button`); console.log(el.value); } getButtonValue(2,3);
table tr:nth-child(2) td:nth-child(3) button { border: 1px solid red; border-radius: 3px }
<table> <tr> <td><button name="button" value="1-1">Click</button></td> <td><button name="button" value="1-2">Click</button></td> <td><button name="button" value="1-3">Click</button></td> </tr > <tr> <td><button name="button" value="2-1">Click</button></td> <td><button name="button" value="2-2">Click</button></td> <td><button name="button" value="2-3">Click</button></td> </tr > <tr> <td><button name="button" value="3-1">Click</button></td> <td><button name="button" value="3-2">Click</button></td> <td><button name="button" value="3-3">Click</button></td> </tr > </table>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.