Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 从JQuery中动态创建的数据获取单击事件_Javascript_Html_Ajax_Jquery_Dynamic - Fatal编程技术网

Javascript 从JQuery中动态创建的数据获取单击事件

Javascript 从JQuery中动态创建的数据获取单击事件,javascript,html,ajax,jquery,dynamic,Javascript,Html,Ajax,Jquery,Dynamic,下面的代码是使用Ajax调用动态生成的,并放置在名为studentresults的硬编码div中 <div id="studentresults" class="row span8 offset2"> <table id="tablestudent" class="table table-striped table-hover center-table"> <thead>Heading for my table</thead<

下面的代码是使用Ajax调用动态生成的,并放置在名为studentresults的硬编码div中

<div id="studentresults" class="row span8 offset2">
  <table id="tablestudent" class="table table-striped table-hover center-table">
    <thead>Heading for my table</thead<
      <tbody>
        <tr id="showstudents">
          <td>29041</td>
          <td>jan</td>
          <td>jan</td>
          <td>
            <a class="btn" href="#">View Results »</a>
          </td>
          <td id="29041">
            <a id="29041" class="btn showstudent" href="#">Delete Student » </a>
          </td>
        </tr>
        <tr id="showstudents">
           .... another dynamic record from Ajax...
        </tr>
     </tbody>
  </table>
</div>
但是,这在硬编码的div
容器中有效

 $('#studentresults').click(function () {

 alert('In click');

});

如果动态元素需要使用基于事件传播的事件侦听器,如何访问动态内容

当您使用
$('.showstustudent')。单击(..)
注册事件处理程序时,它会在执行时执行选择器,并且动态元素可能在执行时不存在,因此事件处理程序不会附加到这些元素

$(document).on('click','.showstudent', function(){
    alert('In click');
});

您有多个具有相同id的元素。这不是有效的HTML,您应该修复它,以便可以安全地按id选择元素。非常感谢您的简要解释。还有一个问题。如何获取生成的行中每个行的id?我尝试了var id=($this).id;没有成功。我只是努力。。。。。。。。var id=$(this.attr('id');我可以得到每个点击标签的ID。谢谢你的帮助。@user2522307你可以简单地说
var id=this.id
其中
this
是点击的元素
$(document).on('click','.showstudent', function(){
    alert('In click');
});