我有一个html页面,有多个类似的元素,可以在原地编辑:
The Title
Some description
Count 10
[Edit]
Another Title
Another description
Count 54
[Edit]
...如果单击编辑按钮,文本将通过Ajax替换为表单(页面的其余部分将不会重新加载):
Title: [The Title ]
Description: [Some description]
Count: [10]
[Save] [Cancel]
Another Title
Another description
Count 54
[Edit]
...用表单替换文本和提交表单的事件处理程序对于所有项几乎都是相同的。我的第一种方法是在html标签中使用ID,并将jQuery事件处理程序绑定到这个ID,但这会导致重复的代码。因此,最明显的想法是不要为每个项目重复类似的代码。
如何区分事件处理程序中的项,以便将正确的项替换为表单并提交正确的表单?
发布于 2011-11-03 22:44:18
$('.classYouPutOnAllEditButtons').click(function () {
var that = $(this); // this variable now holds a reference to the Edit button that was clicked, you can use a traversal method (e.g. closest()) to find the form it was in
// do your other stuff
});发布于 2011-11-03 22:44:43
在事件处理程序中,this引用被单击的特定元素。
$(yourSelector).bind('event', function () {
// in here, `this` will be the Edit button.
});https://stackoverflow.com/questions/7996757
复制相似问题