Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
327 views
in Technique[技术] by (71.8m points)

addeventlistener - Integration of Eventlistner in Javascript not working

I dont' know if anybody is willing to help me out on a bit on Javascript? I am working on a Webapp with Flask.
To get the user info and a googlemaps load I have to use pure Javascript.
This is what I got for my onClick event and it works just fine.

// Google Map initialize
function initMap() {
    var map = new google.maps.Map(document.getElementById('map'), {
        zoom: 10,
        center: { lat: 47.2172500, lng: -1.5533600 }
    });

    document.getElementById('submit').addEventListener('click', function () {
        var userQuestion = document.getElementById('userQuestion').value;
        requestQuestionMap( map, userQuestion);
    });
}

Now I also would like that if the user hits the "enter" key that my function works the same way (sending the user search to my API). I tried some solutions but none seem to work for me. I am probably getting something wrong!

my field for user search in HTML is a classic "input" like this:

<form>
    <input type="text" name="question" id="userQuestion" required>
    <input type="button" value="Dit moi tout ! " id="submit">            
</form>

Of course I did some research here and on google and tried out some solution. none of them seem to fit.

question from:https://stackoverflow.com/questions/65844646/integration-of-eventlistner-in-javascript-not-working

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

https://stackoverflow.com/a/57226770/5011051

This will help you.

<form onsubmit="return false">
    <input type="text"/>
    <input type="submit"/>
</form>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...