简体   繁体   English

将按键或事件侦听器添加到输入中,“ TypeError:…addEventListener不是函数”

[英]Add keypress or eventlistener to input, “TypeError: …addEventListener is not a function”

I'm working on a form where I do not want enter/return to submit the form so I used a function like this. 我正在处理一个我不希望输入/返回提交表单的表单,所以我使用了这样的函数。

    $('[name="form"]').keypress(function(e) {
        var charCode = e.charcode || e.keyCode || e.which;
        if (charCode === 13) {
            e.preventDefault();
            return false;
        }
    });

That works, but now I want to assign the enter/return to perform functions on two inputs on the form. 那行得通,但是现在我想分配输入/返回以在表单上的两个输入上执行功能。 I'm totally stuck. 我完全被困住了。

To get the inputs I've tried vanilla js calling by id, jQ calling by id and then a mixer of the two with variables. 为了获得输入,我尝试通过id调用vanilla js,通过id调用jQ,然后将两者与变量进行混合。 I've also tried .keypress, .keydown, .keyup instead of the attachEventListener method. 我还尝试了.keypress,.keydown,.keyup而不是attachEventListener方法。 No matter what I do, I get this error in console. 无论我做什么,我都会在控制台中收到此错误。

"TypeError: ...addEventListener is not a function" (or keypress, keydown etc.) “ TypeError:... addEventListener不是函数”(或按键,击键等)

I've also researched a good deal but can't find any solution. 我也进行了大量研究,但找不到任何解决方案。 I appreciate any suggestions. 我感谢任何建议。 Here is this block of code in it's current form that's giving the trouble. 这是当前形式的这段代码,给您带来麻烦。

    var yelpInput = $('#inputURL');
    var googleInput = $('#googleURL');

    yelpInput.addEventListener("keydown", function(e) {
        if (e.keyCode === 13 ) {                        
            alert('do stuff!');
        }
    });

    // Google
    googleInput.addEventListener("keydown", function(e) {
        if (e.keyCode === 13 ) {    
            alert('do stuff!');
        }
    });

Thanks 谢谢

var yelpInput = $('#inputURL');
var googleInput = $('#googleURL');

yelpInput.keydown(function(e) {
    if (e.keyCode === 13 ) {                        
        alert('do stuff!');
    }
});

// Google
googleInput.keydown(function(e) {
    if (e.keyCode === 13 ) {    
        alert('do stuff!');
    }
});

yelpInput is jQuery wrapped object which does not have addEventListener method. yelpInputjQuery包装的对象,没有addEventListener方法。

Use .on to attach event-handler on jQuery wrapped object or yelpInput[0].addEventListener / yelpInput.get(0).addEventListener to attach event using JavaScript as yelpInput[0] will be an DOMElement not jQuery-wrapped object. 使用.onjQuery包装的对象上附加event-handler ,或者使用yelpInput[0].addEventListener yelpInput.get(0).addEventListener / yelpInput.get(0).addEventListenerJavaScript上附加事件,因为yelpInput[0]将是一个DOMElement而不是jQuery-wrapped对象。

 var yelpInput = $('#inputURL'); var googleInput = $('#googleURL'); yelpInput.on("keydown", function(e) { //-----^^^ if (e.keyCode === 13) { alert('do stuff!'); } }); googleInput.on("keydown", function(e) { //-------^^^ if (e.keyCode === 13) { alert('do stuff!'); } }); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM