2015-06-23 70 views
1

脑部疼痛。动态行添加到表中:添加点击事件不会触发

我有一个动态添加行到表的计时器。当用户点击其中一个动态添加的行时,我想要一个事件触发 。

我用“PortletTableUseButton”类和使用.on(“click”)标记每个,但它不起作用。

$('.PortletTableUseButton').on("click", function() { 
     alert($(this)); 
    }); 
    setInterval(function() { addRow(); }, 2000); 

    var counter = 2042; 
    function addRow() { 
     counter++; 
     var myRow = "<tr class='PortletTableUseButton'><td>BLOCK-" + counter + "</td><td>Location A</td><td >Use</td></tr>"; 

     $("#Portlet #content tr:last").after(myRow); 
    } 
<div id="Portlet" style="position:absolute; top:500px;left:500px; height:200px;"> 
    <div id="content" style="height:80%;"> 
     <table id="PortletTable"> 
      <tr> 
       <td>File Name</td> 
       <td>Location</td> 
       <td>Action</td> 
      </tr> 
     </table> 
    </div> 
</div> 

回答

4

您没有正确使用事件委托。每次

$(document).on('click', '.PortletTableUseButton', function() { ... }); 

http://learn.jquery.com/events/event-delegation/

或者,运行点击功能添加行:试试这个。

+0

谢谢,但没有变化。该事件不会被触发。 – Proveniebam

+0

当然可以。你在控制台中看到任何语法或其他错误?添加行后,您的HTML看起来像什么? – isherwood

+1

谢谢先生。你是对的:你的代码是正确的。感谢您的帮助:我的大脑现在不那么疼。 – Proveniebam

相关问题