简体   繁体   English

输入密钥以搜索无效

[英]Enter key to search not working

I am trying to figure out why my function to run the submit(searchBtn) is not working correctly. 我试图弄清楚为什么我的运行Submit(searchBtn)的功能无法正常工作。 If you have any idea on what is going wrong, I would appreciate the help! 如果您对发生的问题有任何想法,请多多帮助!

$(document).ready(function() {

  //Click searchbtn and run our search

  $('#searchBtn').click(function() {
    // Get value of our searchbar that user inputs
    var searchInput = $('#searchInput').val();
    //reset our textbox when search is called
    $('#searchInput').val('');
    //set our search url with the API and searchInput
    var url = "https://en.wikipedia.org/w/api.php?action=opensearch&search=" + searchInput + "&format=json&callback=?";

      $.ajax({
        data: "GET",
        url: url,
        async: false,
        dataType: "JSON",
        success: function(data) {

           $('#output').html('');
           for (let i = 0; i < data[1].length; i += 1) {
            $('#output').append("<li><a href=" + data[3][i] + ">" + data[1][i] + "</a><p>" + data[2][0] + "</p></li>");
           }

        },
        error: function(errorMessage) {
          alert("There was a problem retrieving your results.");
        }
    })

This is the function to run the click function when the enter key is released. 释放回车键时运行点击功能的功能。 It appears directly after the code example above. 它直接出现在上面的代码示例之后。

    $('#searchInput').keyup(function(event) {
        if (event.which === 13) {
          $('#searchBtn').click();
      }
    });
  });
});

This is how I'd do it, it removes the programmatic click and just runs the function. 这就是我要这样做的方式,它删除了程序化的单击,只运行了函数。

 $(document).ready(function() {

   //Click searchbtn and run our search

   function search() {
    // Get value of our searchbar that user inputs
    var searchInput = $('#searchInput').val();
    //reset our textbox when search is called
    $('#searchInput').val('');
    //set our search url with the API and searchInput
    var url = "https://en.wikipedia.org/w/api.php?action=opensearch&search=" + searchInput + "&format=json&callback=?";

      $.ajax({
        data: "GET",
        url: url,
        async: false,
        dataType: "JSON",
        success: function(data) {

           $('#output').html('');
           for (let i = 0; i < data[1].length; i += 1) {
            $('#output').append("<li><a href=" + data[3][i] + ">" + data[1][i] + "</a><p>" + data[2][0] + "</p></li>");
           }

        },
        error: function(errorMessage) {
          alert("There was a problem retrieving your results.");
        }
    }

$('#searchBtn').click(search);
$('#searchInput').keyup(function(event) {
    if (event.which === 13) {
      search();
    }
 });
});

Solved with the help of @SethWhite and some people from /r/learnprogramming with the following code: 在@SethWhite和/ r / learnprogramming的帮助下,使用以下代码解决了该问题:

$(document).ready(function() {
   //Click searchbtn and run our search

   function search() {
    // Get value of our searchbar that user inputs
    var searchInput = $('#searchInput').val();

    //reset our textbox when search is called
    $('#searchInput').val('');
    //set our search url with the API and searchInput
    var url = "https://en.wikipedia.org/w/api.php?action=opensearch&search=" + searchInput + "&format=json&callback=?";

      $.ajax({
        data: "GET",
        url: url,
        async: false,
        dataType: "JSON",
        success: function(data) {

           $('#output').html('').addClass('animated slideInUp');

           for (let i = 0; i < 5; i += 1) {
            $('#output').append("<li><a href=" + data[3][i] + ">" + data[1][i] + "</a><p>" + data[2][i] + "</p></li>");
           }

        },
        error: function(errorMessage) {
          alert("There was a problem retrieving your results.");
        }
    })
    $('#output').removeClass();
   }
  $('#searchBtn').click(function(event) {
      event.preventDefault();
      search();
    });
  $('#searchInput').keypress(function(event) {
    if (event.which == 13) {
      event.preventDefault();
      search();
    }
  });
});

Using event.preventDefault(); 使用event.preventDefault(); takes care of this, since each time the ajax is called, we are resetting the #seachInput with '' causing the input form to alert you of an empty form. 可以解决这个问题,因为每次调用ajax时,我们#seachInput使用''重置#seachInput ,从而使输入表单向您发出空表单警报。

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

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