javascript - 在 Jeditable 中使用 onreset 时无法引用 "this"

标签 javascript jquery jeditable

出于某种原因,当我使用 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 and onsubmit, 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/

相关文章:

javascript - 如何使用 Node.js 和 socket.io 设置 gzip 编码?

javascript - 如何正确模拟节点模块以进行 Jest 单元测试

jquery - 如何查看被点击的项目的数量

jquery - 使用 select 时是否可以在 jeditable 中删除提交按钮

javascript - 在 Vue.js 中绑定(bind)数组项

javascript - 仅在使用 CSS 的较长页面中显示按钮

javascript - 当输入类型数字的最后一个字符时,无法获取 '.'

javascript - 在 iphone 的 webview 上自动显示键盘到下一个输入文本

jquery - onblur 事件可以与 Jeditable 和 jQuery UI sortable 一起使用吗?

jquery - jQuery jEditable 取消事件的回调