2012-02-09 101 views
-1

我在jQuery的一个函数,它进入到一个PHP文件,该文件querys数据库返回一个表中的结果,每行有一个按钮, 如何访问按钮的页面了,jQuery打开。这里是一些代码jQuery的表的onclick事件

//function to get table form php file with button on each row 
function show(str) 
{ 

$.post('INCLUDES/gettable.php',{club: str}, 
    function(output) 
    { 

     $('#box').html(output).show(); 
    }); 

} 
//when button from table is clicked do something 
    $("button").click(function() 
    { 
     alert("hello"); 
    } 

任何帮助表示赞赏

+0

你错过了);在您的点击处理程序的末尾。 – jbabey 2012-02-09 14:53:00

回答

1

使用live()或更好on()(自带的jQuery的最新版本)的动态生成HTML /数据:

$("button").on('click', function() 
{ 
    alert("hello"); 
} 

或者

$("button").live('click', function() 
{ 
    alert("hello"); 
} 
+0

我已经这样做了,但由于某种原因,它只适用于.live,你能告诉我为什么会发生这种情况吗? – clonebaby59 2012-02-09 16:24:45

0

它看起来像你正在加载标签动态地删除行,所以它们不会在页面加载时成为dom的一部分。

,你应该使用。对()函数来获得访问这些 - 看jQuery API docs