我想做的是:每次我点击一个表格行,它就进入编辑模式。当我单击另一行时,我以前单击的行退出编辑模式,而我刚才单击的行则进入编辑模式。
以下代码位于包含的JavaScript文件中。我有一个使表行可编辑的函数。
function editRow(row) {
/* awesome code here */
}
做好准备..。
$(".editable_title").click(function() {
editRow(this.parentNode);
});`
我试过的是:
$(document).ready(function() { });
之间,但它只适用于第一次单击和第一次编辑提交。但是,如果我再次尝试这样做,它就不能工作了,$(document).ready(function(){ });
。然后在tr的问题上使用onclick
事件:1. The JavaScript is intrusive.
2. If I click on the same row more than once, it keeps on triggering the event multiple times; and if I click in different rows, all of them go into edit mode at the same time.
我知道这只是一个不断的事件侦听器和使用取消绑定的问题。我试过使用.live()
,它工作得很好,但后来我无法让.die()
工作。
你能给我一些建议吗?
发布于 2011-08-28 04:20:58
尝尝这个
$(".editable_title").one(function() {
editRow(this);
});
function editRow(row) {
var rowNode = row.parentNode;
/* awesome code here */
//Attach the click event here again after editing is done
//If you are doing any ajax calls then this should be done in the success callback
$(row).one(function() {
editRow(this);
});
}
发布于 2011-08-28 01:00:42
您可以使用one
而不是click
$('.editable_title').one('click', function() { editRow(this.parentNode); });
然后,在完成编辑时,只对您编辑的行在one
上再次使用.editable_title
。
或者你可以用旗子:
$('.editable_title').click(function() {
var $p = $(this).parent();
if($p.data('editing'))
return;
$p.data('editing', true);
editRow($p[0]);
});
然后,当您完成编辑时,为适当的$x.removeData('editing')
执行一个$x
。
https://stackoverflow.com/questions/7218347
复制相似问题