html按钮绑定点击事件无效,jquery添加的html元素按钮为何不执行类样式绑定的click事件...

代码举例:jquery

更多按钮:ajax

json

点击按钮添加一行文本框和“提交”按钮:(没有问题,能够正常添加)服务器

$(".addMore").click(function(){app

var index = this.id.substring(7,this.id.length);post

//$("#tr"+index).clone().appendTo("#table"+index);this

$("#table"+index).append("

"+url

"+spa

"

"+事件

"

"+

"

");

});

全部“提交”按钮的点击事件:

$(".submitBtn").click(function(){//初始化后添加的jQuery元素的click事件

//议题的编号,也是text的index

var index = this.id.substring(6,this.id.length);

alert("index="+index);

var content = $("#content"+index).val();

var date = $("#date"+index).val();

var result = $("#result"+index).val();

//判断是否有空值

if(content.length != 0 && date.length != 0 && result.length != 0 ){

$.ajax({

url:"addIssueInfo",

type:"post",

data: {issueContent:content,

issueId:index,

issueDate:date,

issueResult:result},

datatype: "json",

success:function(){

alert("添加成功!");

$tr = $("#addBtn"+index).parent("td").parent("tr");

$tr.remove();

//追加行

$("#table"+index).append("

"+content+""+date+""+result+"");

},error:function(){

alert("服务器忙,请稍候再试!");

}

});

}else{

alert("对不起,当前行的每一条信息均不能为空,请补全后提交!");

}

});

问题是,为何点击“更多”触发的事件添加的文本框和“提交”按钮,这个按钮不执行class="submitBtn"的点击事件?

解决:在添加更多一行的时候所产生的 input 和 button 都是动态生成的,因此不能使用 click,要使用 live (jquery 1.7.2 以后的版本不建议使用 live) 或 on

把$(".submitBtn").click(function(){

改成

$(".submitBtn").live('click', function(){

或者

$(".submitBtn").on('click', function(){

记住若是元素在页面初始化的时候不存在,而是以后经过动态生成在页面中的,要对这些元素进行操做,例如 click, blur, keyup, change....,都要使用 on

.on('click', function

.on('blur', function

.on('keyup', function

....

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值