Php jQuery自动完成第一次尝试不起作用

Php jQuery自动完成第一次尝试不起作用,php,jquery,zend-framework,autocomplete,Php,Jquery,Zend Framework,Autocomplete,我已经尝试在我的程序中实现动态自动完成。它在第一次输入后工作正常。但它并没有显示第一次尝试的建议。但是,服务器正在响应自动完成所需的源。这是我的密码 $('.autocomplete').live('keyup', function(){ $this = $(this); var search = $this.val(); $.ajax({ url:'/package/index/search/keyword/

我已经尝试在我的程序中实现动态自动完成。它在第一次输入后工作正常。但它并没有显示第一次尝试的建议。但是,服务器正在响应自动完成所需的源。这是我的密码

       $('.autocomplete').live('keyup', function(){
        $this = $(this);
        var search = $this.val();
        $.ajax({
            url:'/package/index/search/keyword/'+search+'/format/json',
            async: false,
            success: function(res){
                //console.log(res.options);
                //console.log(res.defined_ids);
                staticObject = res.defined_ids;
                $this.autocomplete({
                    source: res.options
                });
            }
        });            
    });
服务器端代码为

       $('.autocomplete').live('keyup', function(){
        $this = $(this);
        var search = $this.val();
        $.ajax({
            url:'/package/index/search/keyword/'+search+'/format/json',
            async: false,
            success: function(res){
                //console.log(res.options);
                //console.log(res.defined_ids);
                staticObject = res.defined_ids;
                $this.autocomplete({
                    source: res.options
                });
            }
        });            
    });
    $keyword = $this->_getParam('keyword');
    $elementDetailModel = new Package_Model_ElementDetail();
    $arr = $elementDetailModel->searchElementDetail($keyword);
    $this->view->options = $arr['options']; // returns in the format array("test2","my new test", "night stay in delux room")
    $this->view->defined_ids = $arr['defined_ids']; // returns in the format array(21::21=>"test2", 22::22=>"my new test", 24::24=>"night stay in delux room")
当我在firebug中记录定义的\u ID和选项时,当我在文本字段中键入“t”时,我得到了以下响应。
选项:

       $('.autocomplete').live('keyup', function(){
        $this = $(this);
        var search = $this.val();
        $.ajax({
            url:'/package/index/search/keyword/'+search+'/format/json',
            async: false,
            success: function(res){
                //console.log(res.options);
                //console.log(res.defined_ids);
                staticObject = res.defined_ids;
                $this.autocomplete({
                    source: res.options
                });
            }
        });            
    });
[“测试2”,“我的新测试”,“在delux房间过夜”]

       $('.autocomplete').live('keyup', function(){
        $this = $(this);
        var search = $this.val();
        $.ajax({
            url:'/package/index/search/keyword/'+search+'/format/json',
            async: false,
            success: function(res){
                //console.log(res.options);
                //console.log(res.defined_ids);
                staticObject = res.defined_ids;
                $this.autocomplete({
                    source: res.options
                });
            }
        });            
    });
已定义的\u ID:

       $('.autocomplete').live('keyup', function(){
        $this = $(this);
        var search = $this.val();
        $.ajax({
            url:'/package/index/search/keyword/'+search+'/format/json',
            async: false,
            success: function(res){
                //console.log(res.options);
                //console.log(res.defined_ids);
                staticObject = res.defined_ids;
                $this.autocomplete({
                    source: res.options
                });
            }
        });            
    });
对象{21::21=“test2”,22::22=“我的新测试”,24::24=“在delux房间过夜”}

       $('.autocomplete').live('keyup', function(){
        $this = $(this);
        var search = $this.val();
        $.ajax({
            url:'/package/index/search/keyword/'+search+'/format/json',
            async: false,
            success: function(res){
                //console.log(res.options);
                //console.log(res.defined_ids);
                staticObject = res.defined_ids;
                $this.autocomplete({
                    source: res.options
                });
            }
        });            
    });

任何帮助都是值得的。提前感谢。

firebug显示的格式不是JSON格式。它是一个数组,可以使用索引访问

       $('.autocomplete').live('keyup', function(){
        $this = $(this);
        var search = $this.val();
        $.ajax({
            url:'/package/index/search/keyword/'+search+'/format/json',
            async: false,
            success: function(res){
                //console.log(res.options);
                //console.log(res.defined_ids);
                staticObject = res.defined_ids;
                $this.autocomplete({
                    source: res.options
                });
            }
        });            
    });
显示输出时,请确保先显示数组,然后再显示它

       $('.autocomplete').live('keyup', function(){
        $this = $(this);
        var search = $this.val();
        $.ajax({
            url:'/package/index/search/keyword/'+search+'/format/json',
            async: false,
            success: function(res){
                //console.log(res.options);
                //console.log(res.defined_ids);
                staticObject = res.defined_ids;
                $this.autocomplete({
                    source: res.options
                });
            }
        });            
    });
例如,关于这个问题,最终数组应该是这样的

       $('.autocomplete').live('keyup', function(){
        $this = $(this);
        var search = $this.val();
        $.ajax({
            url:'/package/index/search/keyword/'+search+'/format/json',
            async: false,
            success: function(res){
                //console.log(res.options);
                //console.log(res.defined_ids);
                staticObject = res.defined_ids;
                $this.autocomplete({
                    source: res.options
                });
            }
        });            
    });
$array['options'] = array("test2", "my new test", "night stay in room");
//Then you should echo the encoded the array to json

echo json_encode($array);

接下来,请确保关闭此请求。

您可能忘记指定上下文服务器端。在控制器的
\u init()
方法中,添加:

       $('.autocomplete').live('keyup', function(){
        $this = $(this);
        var search = $this.val();
        $.ajax({
            url:'/package/index/search/keyword/'+search+'/format/json',
            async: false,
            success: function(res){
                //console.log(res.options);
                //console.log(res.defined_ids);
                staticObject = res.defined_ids;
                $this.autocomplete({
                    source: res.options
                });
            }
        });            
    });
$ajaxContext = $this->_helper->getHelper('AjaxContext');
$ajaxContext->addActionContext('actionName', 'json')
            ->initContext();
并确保用动作控制器名称替换
actionName

       $('.autocomplete').live('keyup', function(){
        $this = $(this);
        var search = $this.val();
        $.ajax({
            url:'/package/index/search/keyword/'+search+'/format/json',
            async: false,
            success: function(res){
                //console.log(res.options);
                //console.log(res.defined_ids);
                staticObject = res.defined_ids;
                $this.autocomplete({
                    source: res.options
                });
            }
        });            
    });
然后,
$this->view->options=$arr['options']
将自动转换为有效的json格式

       $('.autocomplete').live('keyup', function(){
        $this = $(this);
        var search = $this.val();
        $.ajax({
            url:'/package/index/search/keyword/'+search+'/format/json',
            async: false,
            success: function(res){
                //console.log(res.options);
                //console.log(res.defined_ids);
                staticObject = res.defined_ids;
                $this.autocomplete({
                    source: res.options
                });
            }
        });            
    });

有关AjaxContext的更多信息。

可能是服务器问题。你在萤火虫身上看到了什么?你能给我们一把小提琴吗?我在firebug中得到了这样的回应,[“test2”,“我的新测试”,“在delux房间过夜”]res.optionRegistry对象{Zend_View_Helper_Doctype={…},Zend_View_Helper_Placeholder_Registry={…},db={…},更多…}路径“/包/索引”选项[“test2”,“我的新测试”,“在delux房间过夜”]0“test2”1“我的新测试”2“我的新测试”24::24“我的新测试”定义了对象{21::21=“test2”,22::21=“我的新测试”,24::24=“我的新测试”22::21“我的新测试”24::24“我的新测试”24“在消光室的夜间住宿”。……Full response.IMO,
[“test2”,“我的新测试”,“消光室的夜间住宿”]
是数组的格式,而不是json。它应该是这样的“选项”:{“test2”、“我的新测试”、“在delux房间过夜”}使页面返回值,在此格式中,响应应在变量中返回。所以,当我进行json_编码时,我得到了以下不起作用的响应。选项“[“测试2”,“我的新测试”]”@BashantaDahalजी, 您必须回显json编码的变量。除非我看到更多的数据和代码,否则我无法帮助您。谢谢您的回复。我还需要定义的_id,因为我需要在每个请求之后分配变量staticObject=res.defined_id;这是服务器端的代码<代码>代码$keyword=$this->_getParam('keyword')$elementDetailModel=新包_模型_ElementDetail()$arr=$elementDetailModel->searchElementDetail($keyword)//$arr['options']以数组形式返回('test','my new test')/$arr['defined_id']以数组形式返回('2'=>'test','23'=>'my new test')$this->view->options=$arr['options']$此->视图->定义的\u id=$arr['defined\u id'];谢谢你的回复。我做了与你提到的完全相同的事情。但它仍然不起作用。它在第一次尝试后工作。我不知道为什么在第一次尝试后自动完成不起作用。这很奇怪,我们需要更多关于您的问题的信息来帮助您(Firebug怎么说?)。还有,你考虑过使用吗?(
ZendX\u JQuery\u Form\u Element\u AutoComplete
)我已经编辑了这个问题。希望你能对我的问题有更多的了解。
       $('.autocomplete').live('keyup', function(){
        $this = $(this);
        var search = $this.val();
        $.ajax({
            url:'/package/index/search/keyword/'+search+'/format/json',
            async: false,
            success: function(res){
                //console.log(res.options);
                //console.log(res.defined_ids);
                staticObject = res.defined_ids;
                $this.autocomplete({
                    source: res.options
                });
            }
        });            
    });