gpt4 book ai didi

javascript - 当输入错误时,jQuery自动完成ajax无法自动更正

转载 作者:行者123 更新时间:2023-12-03 02:11:28 25 4
gpt4 key购买 nike

我正在尝试使用Flask,Elasticsearch和jQuery制作一个类似于Google搜索栏的项目,该项目将根据给出的输入自动提出建议,并在给出错误的输入时也自动给出正确的建议。自动拼写正确,拼写正确,但输入错误时,来自Elasticsearch的正确建议数据出现在浏览器控制台中,但未出现在自动完成下拉列表中。我使用PySpark将数据插入了Elasticsearch。我认为问题与JS文件有关,但不知道这是我的JS文件还是jquery-ui文件。我究竟做错了什么?
JS:

    $(document).ready(function () {
const $source = document.querySelector('#source');
const $result = document.querySelector('#result');

const typeHandler = function (e) {
$result.innerHTML = e.target.value;
console.log(e.target.value);

$.ajax({
url: "/ajax_call",
type: 'POST',
dataType: 'json',
data: { 'data': e.target.value },
success: function (html) {
var data = html.aggregations.auto.buckets
var bucket = []

$.each(data, (index, value) => {
bucket.push(value.key)
});

console.log(bucket)
$("#source").autocomplete({
source: bucket
});
}
});
}
$source.addEventListener('input', typeHandler)
});
正确输入:
Correct Input

输入错误:
Incorrect Input

输入错误的正确数据
Correct data for Incorrect Input

最佳答案

考虑以下示例。

$(function() {
const $source = $('#source');
const $result = $('#result');

$source.autocomplete({
source: function(request, response) {
$.ajax({
url: "/ajax_call",
type: 'POST',
dataType: 'json',
data: {
'data': request.term
},
success: function(html) {
var data = html.aggregations.auto.buckets;
var bucket = [];

$.each(data, (index, value) => {
bucket.push(value.key);
});

console.log(bucket);
response(bucket);
}
});
}
});
});
查看更多:
  • https://jqueryui.com/autocomplete/#remote-jsonp
  • https://api.jqueryui.com/autocomplete/#option-source
  • 关于javascript - 当输入错误时,jQuery自动完成ajax无法自动更正,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64588218/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com