日期:2014-05-16  浏览次数:20487 次

jquery append onclick无效
$('#demoTable').append("<tr><td><input name='' type='button' value='test' onclick ='alert(123)'/></td></tr>");

以上,点击按钮没有反应,

用这个是有效的
$("#btn0").click(function () {
alert("test");
});

可是现在我的button个数是不确定的,不能用上面的方法监听

我希望能实现如下效果
$("#demoTable").append("<input type='button' id='btn1' value='按扭' onclick='test(this)' />");

function test(e) {
            alert($(e).attr('id'));
            $(e).hide();
        } 

可是我这样写点击按钮是没有效果的,求大神帮助,万分感谢!  
jquery? onclick

------解决方案--------------------
测试时可以的啊。
<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js">
</script>
<script>
$(document).ready(function(){
  $("p").click(function(){
  $("#demoTable").append("<input type='button' id='btn1' value='按扭' onclick='test(this)' />");


  });
});
function test(e) {
               alert($(e).attr('id'));
            $(e).hide();
        } 
</script>
</head>
<body>
<table id="demoTable">
</table>
<p>如果您点击我,我会消失。</p>
<p>点击我,我会消失。</p>
<p>也要点击我哦。</p>
</body>
</html>

------解决方案--------------------

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type='text/javascript'>
$(function(){
for(var i=0;i<5;i++){
$('#demoTable').append("<tr><td><input name='' type='button' value='test'/></td></tr>");
}
$('#demoTable input').live('click',function(){
alert(123);
});
})
</script>

<table id="demoTable">
</table>


------解决方案--------------------
1楼的要改一改:
<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js">
</script>
<script>
$(document).ready(function(){
  $("p").click(fu