php - 在我的 ajax 成功中使用自动完成事件
问题描述
我想在我的应用程序中使用自动完成功能。我正在尝试使用 jquery UI 完成,但没有任何反应。我制作了一个 ajax 来获取用户编写的具有特定变量的所有列。查询正在运行,我的数组和我的所有列都从服务器返回。有了这个查询响应,我尝试在成功的 ajax 中执行 jquery 自动完成,但正如我所说的,什么也没发生。
你有想法吗?
function autoCompleteRegate(){
$("#code_regate").keyup(function() {
// AJAX de l'auto-complete
var source = '/gestion/gestDepot/ajaxautocompleteregate';
var codeRegate = $("#code_regate").val();
$.ajax({
type : "POST",
url : source,
async : false,
dataType : 'json',
data : {
'codeRegate' : codeRegate
},
success : function(response) {
var availableTags = response;
$("#code_regate").autocomplete({
source: availableTags
});
}
});
});
public function ajaxautocompleteregateAction()
{
$this->_helper->layout->disableLayout();
$this->_helper->viewRenderer->setNoRender();
$params = $this->_getAllParams();
$codeRegate = $params['codeRegate'];
$oDepotService = new Services_Depot();
$response = $oDepotService->searchCodeRegate($codeRegate);
echo json_encode($response);
}
解决方案
您必须直接传递cd_regate
数组而不是多维数组。一种解决方法是您可以在后端处理 json 输出:
public function ajaxautocompleteregateAction()
{
$this->_helper->layout->disableLayout();
$this->_helper->viewRenderer->setNoRender();
$params = $this->_getAllParams();
$codeRegate = $params['codeRegate'];
$oDepotService = new Services_Depot();
$response = $oDepotService->searchCodeRegate($codeRegate);
$json = [];
foreach ($response as $key => $value) {
array_push($json, $value->cd_regate); // the output will be "[774970, 774690, 774700,... ]"
}
echo json_encode($json);
}
推荐阅读
- wordpress - Wordpress 中的联系表格 7 - 如何隐藏电子邮件中的空白字段?
- reactjs - 无法让 npm 运行启动 testcafe 的脚本
- lua - 为什么这段代码意味着所有表共享一个元表
- javascript - jQuery 在单击下一步时显示和隐藏表单
- azure - 将 DataFactory 连接到 Lotus notes DB
- powerbi - Power BI - 应用筛选器时,度量随机返回空白
- javascript - 参数值未在 JavaScript 中传递
- node.js - 如何将流通过管道传输到节点中的文件描述符?
- node.js - 如何以角度和表达方式从url获取参数?
- import - pycharm 在更新 python 路径时崩溃