【问题标题】:Get ID of the clicked element获取被点击元素的ID
【发布时间】:2016-04-15 20:59:54
【问题描述】:

如何从html()获取id,

$('#example1 tbody').on('click', 'td', function () {
    var id = $(this).html();

    alert(id);
});

结果是警报undefined?

程序运行在 MeteorJS 框架上。

【问题讨论】:

  • 要获取点击的<td>的ID。
  • 使用$(this).attr('id')
  • $(this).attr('id')?
  • 事件被触发了吗?你收到alert了吗?

标签: jquery meteor


【解决方案1】:
$('#example1 tbody tr').each(function(){
   $(this).find("td").each(function(){
     $(this).on( 'click', function () {
      var id  = $(this).attr("id");
      alert(id);
   });
  });
});

Try this fiddle

【讨论】:

  • 你要的是td的id对,你要的是input标签的id??
  • 我认为你可以为它粘贴任何小提琴
  • 看看这个小提琴添加了
【解决方案2】:

在 Meteor 中,您通常不应该使用普通的 jQuery 事件。请改用模板事件。此外,使用 data-action 参数而不是 id 和类来附加事件是一个好习惯:

Template.myTemplate.events({
  'click [data-action~=alert]': function(e, t) {
    var id = $(e.currentTarget).attr('id');
    alert(id);
  },
});

【讨论】:

    【解决方案3】:

    试试这个:

    $(document).on('click', '#example1 td', function () {
        var id = $(this).attr('id');
        alert(id);
    });
    

    【讨论】:

      猜你喜欢
      • 2012-04-12
      • 1970-01-01
      • 1970-01-01
      • 2016-06-30
      • 2013-08-22
      • 1970-01-01
      • 2022-07-19
      • 1970-01-01
      • 2012-02-15
      相关资源
      最近更新 更多