我已经尝试解除绑定点击事件,但有时会有时两次触发5次!现在有点厌倦了!
来自modal.asp的代码
- $("input[name=add_associate]").live("click",function(){
- var addassociateID = $(this).attr("id")
- $.ajax({
- type: 'POST',url: '/data/watchlist_save.asp',data: {m : 'share_watchlist_add',watchListID : <%=WatchListID%>,a : addassociateID},async:true,success: function(data) {
- $(".associate_users").load("/data/sub_watch_members.asp?watchListID=<%=WatchListID%>",{cache:false},function() {
- $(".search_note").html(data)
- $(this).unbind('click').bind('click',handler);
- })
- },error: function(data){
- $(".search_note").html(data)
- }
- });
- })
更新:
基本上我将以下代码调用到.associate_users中
- <div id="associate_list">
- <div class="associate_record">
- <div class="left" style="padding:8px;"><img src="../imgs/nopic-m.png" style="width:30px;height:30px;" class="img_border" /></div>
- <div class="left" style="padding-top:15px;">5)Neil Burton</div>
- <div class="right" style="padding-top:10px;margin-right:5px;"><input type="button" class="btn-blue" name="add_associate" id="890" value="Add"></div>
- <div style="clear:both;"></div>
- </div>
- <div style="clear:both;"></div>
- </div>
更多信息:
这只发生在我触发事件时,关闭模态对话框,然后用不同的watchListID重新打开它
数据结构:
> main.asp:LOADS>
> modal.asp:modal.asp包含该页面上面两个div的jquery,其中包含panel1.asp和panel2.asp数据…
> panel1.asp:包含上面的HTML …
> panel2.asp:没有任何相关的…只是纯HTML。
解决方法
注意你的分号,确保你用一个命令结束,稍后会让你头疼。
对于由live()绑定的事件,它们必须通过调用die()来解除绑定。它具有与unbind()相同的参数。看看documentation。
- function ajaxHandler(){
- var addassociateID = $(this).attr("id");
- var $this = $(this);
- $this.die('click');
- $.ajax({
- type: 'POST',async: true,success: function(data) {
- $(".associate_users").load("/data/sub_watch_members.asp?watchListID=<%=WatchListID%>",function(){
- $(".search_note").html(data);
- $this.bind('click',handler);
- });
- },error: function(data){
- $(".search_note").html(data);
- $this.live('click',ajaxHandler);
- }
- });
- }
- $("input[name=add_associate]").live("click",ajaxHandler);
编辑:忘记添加重点。您必须在点击处理程序中解除直播活动,并将其重新绑定出错,就像@stefan建议一样。
还要确保将此对象保存在变量中,因为它不是指向ajax回调函数中的DOM元素。或者,您可以使用您的ajax请求的上下文属性,查看documentation。