简体   繁体   English

如何在JSP中实现机场自动完成功能?

[英]How can I implement airport auto-complete function in JSP?

I am trying to implement a code snippet from codepen( https://codepen.io/anon/pen/EGqXxE ) into my java web application. 我正在尝试将codepen( https://codepen.io/anon/pen/EGqXxE )的代码片段实现到我的Java Web应用程序中。 However, when I load the page, there is no suggestion and nothing seems to happen. 但是,当我加载页面时,没有任何建议,并且似乎什么也没有发生。 Any ideas ? 有任何想法吗 ?

I added the javascript code into an auto-complete.js and css code (though, I do not plan on using this css later) into an auto-complete.css file and imported them in my jsp, but it does not seem to work. 我将javascript代码添加到auto-complete.js和css代码中(尽管我不打算稍后再使用此css)到auto-complete.css文件中,并将它们导入到我的jsp中,但似乎不起作用。 Furthermore, I found some imported js scripts in this codepen(not very familiar with codepen), so I tried importing those as well, but that didn't work either. 此外,我在该Codepen中找到了一些导入的js脚本(对Codepen不太熟悉),因此我也尝试导入这些脚本,但这也不起作用。

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Upload Page</title>
        <link type="text/css" rel="stylesheet" href="<c:url value="/resources/css/auto-complete.css" />" >

</head>
    <body>

        <div class="form-group">
          <label class="control-label">Enter an Airport</label>
          <input id="autocomplete" class="autocomplete-airport" type="text" />
        </div>
        <script src="<c:url value="/resources/js/auto-complete.js" />"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.16.1/lodash.min.js"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
        <script src="https://unpkg.com/fuse.js@2.5.0/src/fuse.min.js"></script>
        <script src="https://screenfeedcontent.blob.core.windows.net/html/airports.js"></script>
    </body>
</html>

I am expecting to see suggested airports as shown in this codepen demo here: https://codepen.io/anon/pen/EGqXxE . 我希望在此Codepen演示中看到建议的机场, 网址为https ://codepen.io/anon/pen/EGqXxE。

Codepen credits: CAPTAIN ANONYMOUS. Codepen积分:CAPTAIN ANONYMOUS。

To get auto complete data,implement following way. 要获取自动完成的数据,请按照以下方式实现。

Ex. 防爆。 index.jsp 的index.jsp

<!DOCTYPE html>
<html lang="en">    
<head>
<meta charset="UTF-8">
<title>Airport Autocomplete</title>    
<link rel="stylesheet"
    href="https://cdnjs.cloudflare.com/ajax/libs/normalize/5.0.0/normalize.min.css">    
<link rel="stylesheet" href="css/style.css"> 
</head>
<body>
    <div class="form-group">
        <label class="control-label">Enter an Airport</label> <input
            id="autocomplete" class="autocomplete" type="text" />
    </div>
    <div class="form-group">
        <label class="control-label">Enter something else</label> <input
            id="autocomplete2" class="autocomplete" type="text" />
    </div>
    <div class="form-group">
        <label class="control-label">Another field (so we can test TAB
            behavior)</label> <input type="text" />
    </div>
    <script
        src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.16.1/lodash.min.js'></script>
    <script
        src='https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.0/jquery.min.js'></script>
    <script src='https://unpkg.com/fuse.js@2.5.0/src/fuse.min.js'></script>
    <script
        src='https://screenfeedcontent.blob.core.windows.net/html/airports.js'></script>

css/style.css CSS / style.css文件

body {
  margin: 50px;
}

.form-group {
  margin-bottom: 20px;
}

.control-label {
  display: block;
}

.autocomplete-wrapper {
  position: relative;
}
.autocomplete-wrapper input {
  width: 100%;
}

.autocomplete-results {
  position: absolute;
  background: white;
  z-index: 1;
  top: 100%;
  left: 0;
  font-size: 13px;
  border: solid 1px #ddd;
  border-top-width: 0;
  border-bottom-color: #ccc;
  box-shadow: 0 5px 10px rgba(0, 0, 0, 0.2);
}

.autocomplete-result {
  padding: 12px 15px;
  border-bottom: solid 1px #eee;
  cursor: pointer;
}

.autocomplete-result:last-child {
  border-bottom-width: 0;
}

.autocomplete-location {
  opacity: .8;
  font-size: smaller;
}

.autocomplete-results[data-highlight='0'] > :nth-child(1) {
  color: white;
  background: #26C9FF;
  border-bottom-color: #26C9FF;
  outline: solid 1px #00b6f2;
}

.autocomplete-results[data-highlight='1'] > :nth-child(2) {
  color: white;
  background: #26C9FF;
  border-bottom-color: #26C9FF;
  outline: solid 1px #00b6f2;
}

.autocomplete-results[data-highlight='2'] > :nth-child(3) {
  color: white;
  background: #26C9FF;
  border-bottom-color: #26C9FF;
  outline: solid 1px #00b6f2;
}

.autocomplete-results[data-highlight='3'] > :nth-child(4) {
  color: white;
  background: #26C9FF;
  border-bottom-color: #26C9FF;
  outline: solid 1px #00b6f2;
}

.autocomplete-results[data-highlight='4'] > :nth-child(5) {
  color: white;
  background: #26C9FF;
  border-bottom-color: #26C9FF;
  outline: solid 1px #00b6f2;
}

.autocomplete-results[data-highlight='5'] > :nth-child(6) {
  color: white;
  background: #26C9FF;
  border-bottom-color: #26C9FF;
  outline: solid 1px #00b6f2;
}

.autocomplete-results[data-highlight='6'] > :nth-child(7) {
  color: white;
  background: #26C9FF;
  border-bottom-color: #26C9FF;
  outline: solid 1px #00b6f2;
}

.autocomplete-results[data-highlight='7'] > :nth-child(8) {
  color: white;
  background: #26C9FF;
  border-bottom-color: #26C9FF;
  outline: solid 1px #00b6f2;
}

js/index.js JS / index.js

var options = {
  shouldSort: true,
  threshold: 0.4,
  maxPatternLength: 32,
  keys: [{
    name: 'iata',
    weight: 0.5
  }, {
    name: 'name',
    weight: 0.3
  }, {
    name: 'city',
    weight: 0.2
  }]
};

var fuse = new Fuse(airports, options)

$('.autocomplete').each(function() {
  var ac = $(this);

   ac.on('click', function(e) {
    e.stopPropagation();
  })
  .on('focus keyup', search)
  .on('keydown', onKeyDown);

  var wrap = $('<div>')
    .addClass('autocomplete-wrapper')
    .insertBefore(ac)
    .append(ac);

    var list = $('<div>')
      .addClass('autocomplete-results')
      .on('click', '.autocomplete-result', function(e) {
        e.preventDefault();
        e.stopPropagation();
        selectIndex($(this).data('index'), ac);
    })
    .appendTo(wrap);
});

$(document)
  .on('mouseover', '.autocomplete-result', function(e) {
    var index = parseInt($(this).data('index'), 10);
    if (!isNaN(index)) {
      $(this).attr('data-highlight', index);
    }
  })
  .on('click', clearResults);

function clearResults() {
  results = [];
  numResults = 0;
  $('.autocomplete-results').empty();
}

function selectIndex(index, autoinput) {
  if (results.length >= index + 1) {
    autoinput.val(results[index].iata);
    clearResults();
  }  
}

var results = [];
var numResults = 0;
var selectedIndex = -1;

function search(e) {
  if (e.which === 38 || e.which === 13 || e.which === 40) {
    return;
  }
  var ac = $(e.target);
  var list = ac.next();
  if (ac.val().length > 0) {
    results = _.take(fuse.search(ac.val()), 7);
    numResults = results.length;

    var divs = results.map(function(r, i) {
        return '<div class="autocomplete-result" data-index="'+ i +'">'
             + '<div><b>'+ r.iata +'</b> - '+ r.name +'</div>'
             + '<div class="autocomplete-location">'+ r.city +', '+ r.country +'</div>'
             + '</div>';
     });

    selectedIndex = -1;
    list.html(divs.join(''))
      .attr('data-highlight', selectedIndex);

  } else {
    numResults = 0;
    list.empty();
  }
}

function onKeyDown(e) {
  var ac = $(e.currentTarget);
  var list = ac.next();
  switch(e.which) {
    case 38: // up
      selectedIndex--;
      if (selectedIndex <= -1) {
        selectedIndex = -1;
      }
      list.attr('data-highlight', selectedIndex);
      break;
    case 13: // enter
      selectIndex(selectedIndex, ac);
      break;
    case 9: // enter
      selectIndex(selectedIndex, ac);
      e.stopPropagation();
      return;
    case 40: // down
      selectedIndex++;
      if (selectedIndex >= numResults) {
        selectedIndex = numResults-1;
      }
      list.attr('data-highlight', selectedIndex);
      break;

    default: return; // exit this handler for other keys
  }
  e.stopPropagation();
  e.preventDefault(); // prevent the default action (scroll / move caret)
}

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

相关问题 HowTo:在.js文件中工作时,如何将“功能”默认为“功能”作为自动完成的第一个选项 - HowTo: How can I default “function” not “Function” as first option for auto-complete when working in .js files 我如何将输入修剪到JQuery自动完成框? - How would I trim the input to a JQuery auto-complete box? 如何创建自动完成的组合框? - How to create an auto-complete combobox? javascript-如何从自动完成功能获取onChange事件的完整值 - javascript - how to get complete value for onChange event from auto-complete function 淘汰赛自动完成 - Knockout auto-complete 使用CodeMirrror自动完成 - Auto-complete with CodeMirrror 使用此Google Maps API自动完成示例。 可以修改此功能以获取经度和纬度吗? - Using this google maps API auto-complete example. Can this function be modified get the longitude and latitude? 我可以用少量代码创建一个自动完成的下拉文本框吗? - Can I create an auto-complete dropdown text-box in angular with little amount of code? 如何在 function 之后编码“(”时关闭 VS Code 上的 Intellisense 自动完成功能 - How to turn off the auto-complete of Intellisense on VS Code when coding '(' after function 如何自动将股票Google搜索添加到我自己的网站上**? - How do I add the stock Google search to my own website **with auto-complete**?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM