如何知道点击了哪个按钮

发布于 2024-11-05 20:25:41 字数 267 浏览 0 评论 0原文

在我的页面中,我有很多编辑按钮,每个按钮的名称都以“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

北风几吹夏 2024-11-12 20:25:42
function MyOnSubmit(e){
   e = e || window.event;
   // srcElement for IE, target for w3c
   var target = e.target || e.srcElement;
   if (target.id.indexOf("edit") > -1){
     // an edit button fired the submit event
   }
}

尽管我建议您进一步研究以找到更好的方法来处理编辑和删除按钮(例如使它们与 href=editpage.jsp?id=23 链接)

function MyOnSubmit(e){
   e = e || window.event;
   // srcElement for IE, target for w3c
   var target = e.target || e.srcElement;
   if (target.id.indexOf("edit") > -1){
     // an edit button fired the submit event
   }
}

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)

被翻牌 2024-11-12 20:25:42

我很确定您自己刚刚回答了这个问题...

每个都以“编辑”开头,并以唯一的 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..

╭ゆ眷念 2024-11-12 20:25:42

在所有按钮上绑定单击事件:

for (var i = 0; i < buttons.length; i++) {
    var button = buttons[i];
    if (button.addEventListener) {
        button.addEventListener('click', handler, false);
    }
    else {
        button.attachEvent('onclick', handler);
    }
}

在事件处理程序中,获取 Event 对象,然后获取目标:

function handler(e) {
    e = e || window.event;
    // srcElement for IE, target for w3c
    var target = e.target || e.srcElement;
    var id = target.name.substring(4);
    /* your code rely on id */
}

bind click event on all button:

for (var i = 0; i < buttons.length; i++) {
    var button = buttons[i];
    if (button.addEventListener) {
        button.addEventListener('click', handler, false);
    }
    else {
        button.attachEvent('onclick', handler);
    }
}

in your event handler, get the Event object, and then get the target:

function handler(e) {
    e = e || window.event;
    // srcElement for IE, target for w3c
    var target = e.target || e.srcElement;
    var id = target.name.substring(4);
    /* your code rely on id */
}
秋心╮凉 2024-11-12 20:25:42

我认为您可能没有正确表达您的问题。如果您使用 jquery,定位按钮就像 $('#id') 一样简单,如果您想存储该按钮上的任何信息,您可以添加属性或使用 jquery.data 函数。

$('#id').attr("pressed", "true");

或者

$('#id').data('pressed', 'true');

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 use jquery.data function.

$('#id').attr("pressed", "true");

Or

$('#id').data('pressed', 'true');
断念 2024-11-12 20:25:41

使用 jQuery 很简单:

$(':button').click(function() {
  // reference clicked button via: $(this)
  var buttonElementId = $(this).attr('id');
});

尝试一下:
http://jsfiddle.net/7YEay/

根据评论中的反馈进行更新

未经测试/伪代码:

$(':submit').click(function(event) {
  var buttonName = $(this).attr('name');

  if (buttonName.indexOf('edit') >= 0) {
    //confirm("some text") logic...
  }

  event.preventDefault();
});

此文档也可能有帮助: http://api.jquery.com/submit/< /a>

This is simple using jQuery:

$(':button').click(function() {
  // reference clicked button via: $(this)
  var buttonElementId = $(this).attr('id');
});

Try it out:
http://jsfiddle.net/7YEay/

UPDATE based on feedback in comments

This is untested/pseudo code:

$(':submit').click(function(event) {
  var buttonName = $(this).attr('name');

  if (buttonName.indexOf('edit') >= 0) {
    //confirm("some text") logic...
  }

  event.preventDefault();
});

This documentation may be helpful too: http://api.jquery.com/submit/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文