如何将搜索按钮添加到Jquery自动完成

时间:2016-06-27 09:21:47

标签: javascript jquery button jquery-plugins

This 是我的博客。

这是代码

<!doctype html>
Jquery Auto Complete

  <link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css" rel="stylesheet" type="text/css"/>
  <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js">
  <script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js">

  <script>
  $(document).ready(function() {
    $("input#autocomplete").autocomplete({
    source: [
             { value: "NYC", url: 'http://www.nyc.com' }, 
             { value: "LA", url: 'http://www.la.com' },
             { value: "Philly", url: 'http://www.philly.com' },
             { value: "Chitown", url: 'http://www.chitown.com' },
             { value: "DC", url: 'http://www.washingtondc.com' },
             { value: "SF", url: 'http://www.sanfran.com' },
             { value: "Peru", url: 'http://www.peru.com' }
        ],
        select: function (event, ui) {
            window.location = ui.item.url;
        }

});
  });
  </script>



<input id="autocomplete" />


</!doctype>

现在,当用户搜索查询NYC时以及何时选择它是重定向到网址,但我的目的是在用户点击搜索按钮时将搜索按钮放在旁边,然后应该重定向。我希望提前感谢我的问题。

1 个答案:

答案 0 :(得分:0)

完整更新的代码

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js">
</script>

  <link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css" rel="stylesheet" type="text/css"/>
  <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script>
  <script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js"></script>

<script>
var selectedItemUrl = "";
$(function() {

  var source = [{
    value: "NYC",
    url: 'http://www.nyc.com'
  }, {
    value: "LA",
    url: 'http://www.la.com'
  }, {
    value: "Philly",
    url: 'http://www.philly.com'
  }, {
    value: "Chitown",
    url: 'http://www.chitown.com'
  }, {
    value: "DC",
    url: 'http://www.washingtondc.com'
  }, {
    value: "SF",
    url: 'http://www.sanfran.com'
  }, {
    value: "Peru",
    url: 'http://www.peru.com'
  }];

  $("#autocomplete").autocomplete({
    minLength: 0,
    source: source,
    select: function(event, ui) {
      selectedItemUrl = ui.item.url
    }
  })

});

function SearchItem(e){
if(selectedItemUrl!="")
    window.location=selectedItemUrl
    else
    alert("select something to search")

}


</script>


</head>
<body>
<input id="autocomplete" />
<button onclick='SearchItem()'>
Search
</button>

</body>
</html>

如果现在帮助你,请标记为答案:0,:)