简体   繁体   中英

Submit a JQuery form with Enter key press

I've tried various solutions, but none are suitable for my particular case! How can I make it so that when the user presses 'Enter' the form submits & searches.

I am using rotten tomatoes API with AJAX by the way.

FORM

<form name="myform" action="" method="GET"><h3 class="white">Search for a movie here!:</h3><br> 
    <input type="text" id="inputbox" value="">&nbsp;
    <input type="button" id="button" value="Go!" onClick="filmlist(this.form)">
</form>

ajax.js

function filmlist(form) {
    $('#films table').empty(); //removes previous search results before adding the new ones.
    var apikey = "APIKEY";
    var baseUrl = "http://api.rottentomatoes.com/api/public/v1.0";
    var moviesSearchUrl = baseUrl + '/movies.json?apikey=' + apikey;
    var query = form.inputbox.value; //uses the value from the input box as the query search
    $(document).ready(function () {
        // sends the query
        $.ajax({
            url: moviesSearchUrl + '&q=' + encodeURI(query),
            dataType: "jsonp",
            success: searchCallback
        });
    });
    // receives the results
    function searchCallback(data) {
        $('#films table').append('Found ' + data.total + ' results for ' + query);
        var movies = data.movies;
        $.each(movies, function (index, movie) {
            $('#films table').append('<tr><td width="70" rowspan="2"><a href="' + movie.links.alternate +
                '" title="Click here to view film information for ' + movie.title + '."><img class="ajaximage" src="' + movie.posters.thumbnail + '" /></a></td><td class="ajaxfilmlisttitle"><h3><a href="' + movie.links.alternate +
                '" title="Click here to view film information for ' + movie.title + '.">' + movie.title + '</a></h3>Release year: ' + movie.year + '</td></tr><tr><td class="ajaxfilmlistinfo">Audience Score: ' + movie.ratings.audience_score +
                '<br>' + 'Cinema Release Date: ' + movie.release_dates.theater +
                '<br>Runtime: ' + movie.runtime + ' minutes</td></tr>');
        });
    }
}

I'll give it a go, a more complete solution for the whole 'Listen to Keypress Enter to submit' which I faced some problems while making it work cross-browser, perhaps this will do it for you.

According to your code, this should work just fine.

  $('#inputbox').live("keypress", function(e) {
     var code = (e.keyCode ? e.keyCode : e.which);
     if (code == 13) {
        e.preventDefault();
        e.stopPropagation();
        $(this).closest('form').submit();
     }
  });

If the user presses enter on any form element, the submit action will be called. You can listen for that event via .submit :

$('#myForm').submit(function(event){
  event.preventDefault(); // stop the actual submit
  // ajax code goes here to submit data
});

With jQuery, I use this short snippet for capturing Enter key press events, and binding a function to it (without actually resorting to forms and submitting):

$.fn.pressEnter = function(fn) {
  return this.each(function() {
    $(this).off('enterPress');
    $(this).on('enterPress', fn);
    $(this).keypress(function(e) {
      if (e.keyCode == 13) {
        $(this).trigger("enterPress");
      }
    });
  });
};

Next, call the .pressEnter jQuery extension on your element(s):

$('mySelector').pressEnter(function () {
  console.log('Enter pressed!');
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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