简体   繁体   English

javascript - 当所有按钮命名相同时,获取单击的按钮值

[英]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.

相关问题 如何仅获取在 JavaScript 中具有相同 class 的一组按钮中单击的一个按钮的值? - How to get value of only one button that was clicked in a set of buttons that have the same class in JavaScript? 单击反应中的一个按钮时,如何禁用所有按钮? - How to get all buttons disabled, when one button is clicked in react? 当所有值都相同时,如何检查单击了哪个按钮 - How to check which button was clicked when all have the same value 获取点击按钮的值 javascript - get value of the clicked button javascript Javascript:当所有按钮都被点击时 function - Javascript: When all buttons are clicked function 单击按钮时,获取相同类别但复选框的差异ID的值 - Get value of same class but diff id of checkbox, when a button is clicked 当我单击“编辑”按钮时,所有按钮都使用相同的用户名单击 - When i click edit button all the buttons are getting clicked with same username 在单击按钮时获取按钮的值,javascript - Getting the value of a button when the button is clicked, javascript Javascript函数根据单击的按钮动态创建具有返回值的按钮 - Javascript function to dynamically create buttons with return value based on button clicked 从javascript中单击的多个按钮获取价值 - get value from multiple button clicked in javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM