出于某种原因,当我使用 onreset 处理程序时,我无法将正在编辑的元素引用为 $(this)
。
但是,我可以在回调中使用 use $(this)
。我确信 onreset 有效,因为我已经做了警报。此外,当我对 $(this).attr('id')
发出警报时,我得到“未定义”。
发生什么事了?
代码
$('.edit').editable('ajax/save.php?editnotetext', {
type : 'mce',
submit : '<button class="save_button">Save</button>',
cancel : '<button class="cancel_button">Cancel</button>',
event: 'dblclick',
placeholder : 'Doubleclick to edit...',
indicator : 'Saving...',
tooltip : 'Doubleclick to edit...',
onblur: 'custom',
callback : function(){
console.log('unlocked');
$.post('ajax/save.php?unlocknotetext', {"id" : $(this).attr('id')});
},
onreset : function(){
console.log('unlocked');
//myId = $(this).attr('id');
//alert(myId); this shows up as undefined!
//alert("onreset works!");
$.post('ajax/save.php?unlocknotetext', {"id" : $(this).attr('id')});
}
});
最佳答案
this comment似乎可以解释一下:
It seems inside
onreset
andonsubmit
,this
points to the form, not its container, so you have to use$(this).parent()
instead.
一个简单的解决方法是这样做:
$('.edit').each(function() {
var $this = $(this);
$this.editable(... /* use $this instead of $(this) here*/)
});
关于javascript - 在 Jeditable 中使用 onreset 时无法引用 "this",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6967529/