javascript - 使用 id、来自 sqlite 的数据和验证实现自动完成
问题描述
我正在创建我的第一个基于 sinatra 的 Web 应用程序,将 materialize-stepper 用于我的表单,并为其余的前端使用 materialize。我有一个接受用户 2 个用户输入的表单,每个用户输入都有自动完成功能。自动完成的数据直接来自我的数据库。我正在尝试找到一种方法来添加一个功能,其中只能使用来自用户的输入,并且只有在输入来自建议自动完成数据时才能提交表单,并以某种方式添加一个 ID,以便我可以引用每个结果。
需要该 ID,因为该表单从我的数据库中获取机场的起点和目的地,并请求获取包含该信息的航班,如果传递的数据无效,我将无法获得结果。稍后我需要添加一个功能,如果用户想添加该航班,则他可以添加该航班,因此我需要使每个航班“可跟踪”
这是负责自动完成数据和初始化的代码
// document.addEventListener('DOMContentLoaded', function() {
// initialize stepper
const flightStepper = document.querySelector('.stepper');
const stepper = new MStepper(flightStepper);
// initialize the autocomplete
const elems = document.querySelectorAll('.autocomplete');
M.Autocomplete.init(elems, {
limit: 8,
data: {
// pulling all the airports from the database
<%@airport.all.map do |airport|%>
<% if airport.iata_code.split('-')[0].size > 3%>
// "beautifying the autocomplete text for the user and verifying its length"
"<%= airport.name%> ,<%=airport.iata_code.split('-')[0].chop %>": null,
<% else%>
"<%= airport.name%> ,<%=airport.iata_code.split('-')[0] %>": null,
<%end%>
<%end%>
}});
</script>
我的表格是基本表格,取始发机场、目的地等。
感谢任何试图帮助我的人!
解决方案
推荐阅读
- typescript - 空字符串在 React Native 应用程序中呈现为未定义
- entity-framework - 具有特定表的 Scaffold-DbContext 多个 Schema
- json - 如何在 Swif 5 IOS 中发送带有 json 字符串作为参数的帖子并通过 Alamofire 或 URLSession 发布
- group-by - 由 Chunks 提供的 Solr 响应组
- javascript - 使用 cross-env 将 CLI 参数传递给 NPM 脚本
- security - 如何隐藏密码不被打印到命令行?
- python - 计算图像和矢量的“点”积
- mips - 首先在a0中加载整数然后调用“li v0,1”是否正确?
- java - 在独立的springboot java应用程序的introscope中获取JDBC连接监控数据
- python - loss为nan,训练Mask-RCNN多类分割时停止训练