簡體   English   中英

jQuery表單提交API AJAX

[英]JQuery Form Submission API AJAX

有人知道為什么不允許表單提交嗎? 我正在嘗試將Rotten Tomatoes API與用戶搜索功能一起使用。

在PHP頁面中形成表格

<form name="myform" action="" method="GET"><h3>Search for a movie here:</h3><br> 
    <input type="text" id="inputbox" value="">&nbsp;
    <input type="submit" name="submit" value="Go!">

JAVASCRIPT

$('form[name="myform"]').submit(function() {
    $('#films table').empty(); //removes previous search results before adding the new ones.

    var apikey = "frceg2d5djxezaedgm3qq94h";
    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



      // sends the query
      $.ajax({
        url: moviesSearchUrl + '&q=' + encodeURI(query),
        dataType: "jsonp",
        success: searchCallback // if successful, run searchCallback function
      });


    // 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>');
    });
     };
    });

我用此代碼進行了測試,它的工作原理是:

<form name="myform" action="" method="GET">
    <h3>Search for a movie here:</h3>
    <input type="text" id="inputbox" value="" />&nbsp;
    <input type="submit" name="submit" value="Go!" />
</form>

<script>

    $(function(){

        $('form[name="myform"]').on('submit', function(e) {
            e.preventDefault();

            $('#films table').empty(); //removes previous search results before adding the new ones.

            var apikey = "frceg2d5djxezaedgm3qq94h";
            var baseUrl = "http://api.rottentomatoes.com/api/public/v1.0";
            var moviesSearchUrl = baseUrl + '/movies.json?apikey=' + apikey;
            var query = $('#inputbox').val(); //uses the value from the input box as the query search

            // sends the query
            $.ajax({
                url: moviesSearchUrl + '&q=' + encodeURI(query),
                dataType: "jsonp",
                success: searchCallback // if successful, run searchCallback function
            });

            // 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>');
                });
            };
        });
    });
</script>

有幾個問題:

  • form.inputbox.value不起作用,將其更改為$('#inputbox').val()
  • 添加e.preventDefault()以防止提交
  • 缺少一個});
  • $('form[name="myform"]').submit(function() {更改$('form[name="myform"]').on('submit', function(e) {

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM