多个 Jquery .delegate() 问题
我有多个 jquery 委托函数来从列表中删除项目。它们第一次工作正常,但似乎停止为其他删除功能工作的删除功能。
所以我有 3 张桌子,可用性、经验和爱好。如果我点击从爱好列表中删除一个项目,它就会起作用,并禁止其他项目工作。随后从爱好中删除仍然有效。
用firebug查看过它,似乎每次点击从可用性或体验中删除条目都会触发与第一次触发从爱好中删除相同的链接,就好像它已保存它并且不会更改它一样。
$(document).ready(function () {
var i = 1;
var pager;
var pai;
var thetab;
$('#tabs-5').delegate('.delav', 'click', function (e) {
e.preventDefault();
pager = $(this).attr("href");
$("#dialog2").dialog({
autoOpen: false,
width: 600,
modal: true,
buttons: {
"Confirm": function () {
$('#ava_list').html("Loading..");
pai = $('#epa_id').val();
thetab = "availability";
$.get("/_includes/functions.php", {
cache: false,
table: thetab,
pa: pai,
delattr: pager
}, function (data) {
$('#ava_list').html(data);
});
$(this).dialog("close");
},
"Cancel": function () {
$(this).dialog("close");
}
}
});
$("#dialog2").dialog("open");
});
});
$(document).ready(function () {
var i = 1;
var pager;
var pai;
var thetab;
$('#tabs-3').delegate('.delexp', 'click', function (e) {
e.preventDefault();
pager = $(this).attr("href");
$("#dialog2").dialog({
autoOpen: false,
width: 600,
modal: true,
buttons: {
"Confirm": function () {
$('#exp_list').html("Loading..");
pai = $('#epa_id').val();
thetab = "";
$.get("/_includes/functions.php?ms=", {
cache: false,
table: thetab,
pa: pai,
delattr: pager
}, function (data) {
$('#exp_list').html(data);
});
$(this).dialog("close");
},
"Cancel": function () {
$(this).dialog("close");
}
}
});
$("#dialog2").dialog("open");
});
});
$(document).ready(function () {
var i = 1;
var pager;
var pai;
var thetab;
$('#tabs-7').delegate('.delhob', 'click', function (e) {
e.preventDefault();
pager = $(this).attr("href");
$("#dialog2").dialog({
autoOpen: false,
width: 600,
modal: true,
buttons: {
"Confirm": function () {
$('#hob_list').html("Loading..");
pai = $('#epa_id').val();
thetab = "hobby";
$.get("/_includes/functions.php", {
cache: false,
table: thetab,
pa: pai,
delattr: pager
}, function (data) {
$('#hob_list').html(data);
});
$(this).dialog("close");
},
"Cancel": function () {
$(this).dialog("close");
}
}
});
$("#dialog2").dialog("open");
});
});
I have multiple jquery delegate functions to delete items from lists. They work fine for the first time, but seem to stop to the delete function working for the other delete functions.
So I have 3 tables, availability,experience and hobby. If i click to delete an item from the hobby list it works, and diasbles the others from working. Subsequent deletes from hobby still works.
Having looked at it with firebug, it seems each click to delete an entry from availability or experience is triggering the same link that was first triggered to delete from hobby as if it has saved it and won't change it.
$(document).ready(function () {
var i = 1;
var pager;
var pai;
var thetab;
$('#tabs-5').delegate('.delav', 'click', function (e) {
e.preventDefault();
pager = $(this).attr("href");
$("#dialog2").dialog({
autoOpen: false,
width: 600,
modal: true,
buttons: {
"Confirm": function () {
$('#ava_list').html("Loading..");
pai = $('#epa_id').val();
thetab = "availability";
$.get("/_includes/functions.php", {
cache: false,
table: thetab,
pa: pai,
delattr: pager
}, function (data) {
$('#ava_list').html(data);
});
$(this).dialog("close");
},
"Cancel": function () {
$(this).dialog("close");
}
}
});
$("#dialog2").dialog("open");
});
});
$(document).ready(function () {
var i = 1;
var pager;
var pai;
var thetab;
$('#tabs-3').delegate('.delexp', 'click', function (e) {
e.preventDefault();
pager = $(this).attr("href");
$("#dialog2").dialog({
autoOpen: false,
width: 600,
modal: true,
buttons: {
"Confirm": function () {
$('#exp_list').html("Loading..");
pai = $('#epa_id').val();
thetab = "";
$.get("/_includes/functions.php?ms=", {
cache: false,
table: thetab,
pa: pai,
delattr: pager
}, function (data) {
$('#exp_list').html(data);
});
$(this).dialog("close");
},
"Cancel": function () {
$(this).dialog("close");
}
}
});
$("#dialog2").dialog("open");
});
});
$(document).ready(function () {
var i = 1;
var pager;
var pai;
var thetab;
$('#tabs-7').delegate('.delhob', 'click', function (e) {
e.preventDefault();
pager = $(this).attr("href");
$("#dialog2").dialog({
autoOpen: false,
width: 600,
modal: true,
buttons: {
"Confirm": function () {
$('#hob_list').html("Loading..");
pai = $('#epa_id').val();
thetab = "hobby";
$.get("/_includes/functions.php", {
cache: false,
table: thetab,
pa: pai,
delattr: pager
}, function (data) {
$('#hob_list').html(data);
});
$(this).dialog("close");
},
"Cancel": function () {
$(this).dialog("close");
}
}
});
$("#dialog2").dialog("open");
});
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我建议首先进行以下更改,以极大地简化问题,并降低一个对话框与其他对话框发生冲突的可能性:
还有几个问题。
table:
、pa:
和delattr:
属性在传递给 $.get 函数的 get 对象中做什么?它们没有记录在案的$.get()
设置。另外,您的意思是所有三个对话框都具有相同的pa: $('#epa_id').val(),
行吗?下面介绍了如何将所有重复代码折叠到一个通用函数中,并为每个对话框使用唯一的 ID。我还删除了不需要的局部变量:
由于我无法运行此代码,因此我不能说这是否解决了问题,或者这只是正确方向的下一个清理步骤。
I would suggest first making the following changes to drastically simplify the problem and to make it less likely that one dialog can conflict with the others:
And a few questions. What are the
table:
,pa:
anddelattr:
attributes doing in the get object passed to the $.get function? They aren't documented$.get()
settings. Also, do you mean all three dialogs to have identicalpa: $('#epa_id').val(),
lines in them?Here's how you can collapse all your duplicate code into one common function and use unique IDs for each dialog. I've also removed local variables that weren't needed:
Since I have no way of running this code, I can't say whether this fixes the issue or it's just the next cleanup step in the right direction.