使用jQuery获取按钮单击时元素的ID [英] Get id of element on button click using jquery

查看:126
本文介绍了使用jQuery获取按钮单击时元素的ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有动态生成的按钮列表,并且id在运行时生成.如何使用JQuery获取单击按钮的ID.

i have list of dynamic generated buttons and id is generated on run time. how can is get id of clicked button using JQuery.

这是js代码

var btn = " <input type='button' id='btnDel' value='Delete' />";


$("#metainfo").append(txt); //set value of

$("#btnDel").attr("id", "btnDel" + $("#hid").attr("value")); 

推荐答案

以您的示例为例:

$("#btnDel").click(function() {
  alert(this.id);
});

请注意,您无法循环使用已有的代码,ID 必须唯一,否则,您将获得各种副作用,因为它们是无效的HTML.如果您想要任何输入的点击处理程序,请更改选择器,如下所示:

Note that you can't loop the code you have, IDs have to be unique, you'll get all sorts of side-effects if they're not, as it's invalid HTML. If you wanted a click handler for any input, change the selector, like this:

$("input").click(function() {
  alert(this.id);
});

这篇关于使用jQuery获取按钮单击时元素的ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆