如何知道点击了哪个按钮
在我的页面中,我有很多编辑按钮,每个按钮的名称都以“edit”开头,然后是一些 id。我想知道我的任何编辑按钮是否被单击。 详细来说,我有形式。在表单中,我有许多名称以“edit”开头的编辑按钮、名称以“delete”开头的删除按钮和1个添加按钮。都是提交按钮。 form onsubmit 我调用 JavaScript 函数,如果按钮是编辑确认(“某些文本”),否则提交表单。 我怎样才能在 JavaScript 中做到这一点? 我认为给所有这些按钮相同的 id,然后 getElementById 但我如何更改呢?
In my page I have many edit buttons each name starts with "edit" and then some id. I want to know any of my edit buttons is clicked or not.
In details, I have form. In form I have many edit buttons which name starts with "edit" , delete buttons which name starts with "delete" and 1 add button. All are submit buttons. form onsubmit I call JavaScript function in which I want if the button is edit confirm("some text") else submit form.
How can I do that in JavaScript?
I think give all these buttons same id and then getElementById but then how con I change?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
尽管我建议您进一步研究以找到更好的方法来处理编辑和删除按钮(例如使它们与 href=editpage.jsp?id=23 链接)
although i advice you research further to find a better way to handle edit and delete buttons (like making them links with href=editpage.jsp?id=23)
我很确定您自己刚刚回答了这个问题...
每个都以“编辑”开头,并以唯一的 ID 结尾?
所以... $(button).attr("id") 会给你这个。将其存储在变量中?不确定你想做什么..
I'm pretty sure you just answered this yourself...
Each starts with "edit", and ends with a unique ID?
So... $(button).attr("id") would give you that. Store it in a variable? Not sure what you're trying to do..
在所有按钮上绑定单击事件:
在事件处理程序中,获取 Event 对象,然后获取目标:
bind click event on all button:
in your event handler, get the Event object, and then get the target:
我认为您可能没有正确表达您的问题。如果您使用 jquery,定位按钮就像
$('#id')
一样简单,如果您想存储该按钮上的任何信息,您可以添加属性或使用jquery.data
函数。或者
I think you might not have phrased your question correctly. If you are using jquery, locating the button is as easy as
$('#id')
, and if you want to store any information on that button, you can either add an attribute or usejquery.data
function.Or
使用 jQuery 很简单:
尝试一下:
http://jsfiddle.net/7YEay/
根据评论中的反馈进行更新
未经测试/伪代码:
此文档也可能有帮助: http://api.jquery.com/submit/< /a>
This is simple using jQuery:
Try it out:
http://jsfiddle.net/7YEay/
UPDATE based on feedback in comments
This is untested/pseudo code:
This documentation may be helpful too: http://api.jquery.com/submit/