Javascript jQuery:There';这是一种更有效的方法,不是吗;不在那里吗?
我有一个插件,它可以在服务器端对一个表进行排序,并将内容放回一个容器中。我有一个绑定函数,它向表头单元格添加一个单击事件来调用父表单提交。我觉得这不是最好的办法。有什么想法吗Javascript jQuery:There';这是一种更有效的方法,不是吗;不在那里吗?,javascript,jquery,Javascript,Jquery,我有一个插件,它可以在服务器端对一个表进行排序,并将内容放回一个容器中。我有一个绑定函数,它向表头单元格添加一个单击事件来调用父表单提交。我觉得这不是最好的办法。有什么想法吗 $.fn.myplugin = function() { return this.each(function() { var parentform = $(this).parents("form"); var tableidentifier = $(this).attr("id"); var
$.fn.myplugin = function() {
return this.each(function() {
var parentform = $(this).parents("form");
var tableidentifier = $(this).attr("id");
var bindclicks = function() {
parentform.find("table#" + tableidentifier + " thead tr th").click(function() {
// Some code to change the sort column- boring!
parentform.submit();
});
}
bindclicks();
parentform.submit(function() {
$.post(parentform.attr("action"), parentform.serialize(), function(res) {
parentform.find("table#" + tableidentifier).replaceWith($(res));
bindclicks();
})
return false;
});
});
}
我首先调用bindclicks()
函数来设置单击处理程序,然后因为我正在执行replaceWith()
操作,所以我再次调用它来重新绑定这些事件。它是有效的,但我很好奇..而不是这个:
var bindclicks = function() {
parentform.find("table#" + tableidentifier + " thead tr th").click(function() {
parentform.submit();
});
}
bindclicks();
试试这个:
$("table#" + tableidentifier + " thead tr th", parentForm)
.bind('click', function() {
parentForm.submit();
});
如果使用live
而不是bind
,它会将单击处理程序绑定到任何新创建的元素。而不是此:
var bindclicks = function() {
parentform.find("table#" + tableidentifier + " thead tr th").click(function() {
parentform.submit();
});
}
bindclicks();
试试这个:
$("table#" + tableidentifier + " thead tr th", parentForm)
.bind('click', function() {
parentForm.submit();
});
如果使用live
而不是bind
,它会将单击处理程序绑定到任何新创建的元素。您可以使用,这样就不需要每次都重新绑定单击处理程序
$.fn.myplugin = function() {
return this.each(function() {
var parentform = $(this).parents("form");
var tableidentifier = $(this).attr("id");
parentform.delegate("table#" + tableidentifier + " thead tr th", "click", function() {
parentform.submit();
});
parentform.submit(function() {
$.post(parentform.attr("action"), parentform.serialize(), function(res) {
parentform.find("table#" + tableidentifier).replaceWith($(res));
})
return false;
});
});
}
仅显示剪报,这应该可以工作,因为您正在替换
,委派事件绑定到您可以使用的
,因此您不需要每次重新绑定单击处理程序
$.fn.myplugin = function() {
return this.each(function() {
var parentform = $(this).parents("form");
var tableidentifier = $(this).attr("id");
parentform.delegate("table#" + tableidentifier + " thead tr th", "click", function() {
parentform.submit();
});
parentform.submit(function() {
$.post(parentform.attr("action"), parentform.serialize(), function(res) {
parentform.find("table#" + tableidentifier).replaceWith($(res));
})
return false;
});
});
}
仅使用它显示的剪报,这应该可以工作,因为您正在替换
,并且委托事件绑定到
。我不需要在$中发布相同的代码吗?在表单提交后发布回调?在$中发布相同的代码吗?提交表单后发布回调?酷!从未使用过委托
功能-非常非常方便。酷!从未使用过委托
功能-非常非常方便。