[英]Can't reference “this” when using onreset in Jeditable
出于某种原因,当我使用onreset处理程序时,我无法将我正在编辑的元素称为$(this)
。
但是,我可以在回调中使用$(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')});
}
});
这条评论似乎解释了它:
它似乎在
onreset
和onsubmit
,this
指向表单,而不是它的容器,所以你必须使用$(this).parent()
代替。
一个简单的解决方法是这样做:
$('.edit').each(function() {
var $this = $(this);
$this.editable(... /* use $this instead of $(this) here*/)
});
这在onReset部分对我有用:
curr_form = this[0]; //form object
par = curr_form.parentNode; //parent,container object
alert(par.id);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.