weixin_33733810 2016-01-13 10:22 采纳率: 0%
浏览 8

实时搜索Ajax

i have been table site for some time now and i implemented a searching system, but i want it to be real time search so whenever you press a button it will refresh the site. I have little coding experiance so ill ask for some help here.

Here is my ajax code:

function searchDomains() {
    $.ajax({
        type: "POST",
        url: "Ajax/searchDomain.action.php",
        data: {
            domain: $('input[name="domain"]').val(),
            width: $(window).width()
        },
        success: function(data) {
            $("#container_domains").html(data);
        }
    });
}
  • 写回答

1条回答 默认 最新

  • weixin_33691817 2016-01-13 10:26
    关注

    You are almost right, but you need to attach this function to the keyup event of the <input /> and make it unobtrusive too:

    <input type="text" name="search" id="search" />
    <div id="container_domains"></div>
    

    And in the jQuery:

    $(function () {
      $("#search").keyup(function () {
        $.ajax({
          type: "POST",
          url: "Ajax/searchDomain.action.php",
          data: {
            domain: $('input[name="domain"]').val(),
            width: $(window).width()
          },
          success: function(data) {
            $("#container_domains").html(data).show();
          }
        });
      });
    });
    
    评论

报告相同问题?