4

Backbone如果用户在输入文本元素中写入或粘贴字符串,我正在尝试将数据发送到服务器。

在 Backbone 事件中,我想到了这样的事情,但它不起作用:

events:{
    "click .close":"closeResults",
    "keypress input":"fetchData",
    "paste input":"fetchData"
},
fetchData:function (e) {
    var $this = this;
    window.setTimeout(function () {
        if ($.trim(e.target.value).length >= 3) {
            console.log(e.target.value);
            $this.collection.fetch({data: {limit: 10, term:$.trim(e.target.value)}});
        }
    }, 0);
}
4

3 回答 3

6

如果您切换到使用keyup事件而不是keypressand paste,它将适用于通过键盘 (⌘ + vCtrl + v) 粘贴并正常键入。

如果您使用该input事件,即使您右键单击并粘贴它也会起作用(除了与 相同的预期行为keyup)。

更多信息inputhttps ://developer.mozilla.org/en-US/docs/Web/API/window.oninput

于 2013-05-25T04:05:55.067 回答
1

使用 keyup 和 mouseleave(而不是输入)事件处理程序,以便它也可以在 IE 中工作

另请参阅如何使用 JavaScript 检测鼠标右键单击 + 粘贴?

events:{
    "keyup input":"fetchData",
    "mouseleave input":"fetchData" 
},
于 2015-07-17T13:42:57.147 回答
0

看一下

e.originalEvent

_paste_plain_text : function (e) {
    e.preventDefault();
    var text = e.originalEvent.clipboardData.getData("text/plain");
    document.execCommand("insertHTML", false, text);
}
于 2014-06-30T15:37:47.473 回答