首页 > 解决方案 > 在我的 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);
}

网络查询-表格

什么都没有发生的例子

来自服务器的回答

标签: phpjqueryjquery-ui

解决方案


您必须直接传递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);
}

推荐阅读