【问题标题】:jQuery autocomplete not working for the first attemptjQuery自动完成在第一次尝试时不起作用
【发布时间】:2012-04-15 08:55:31
【问题描述】:

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

       $('.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 中控制台记录定义的 ID 和选项时,当我在文本字段中输入“t”时,我得到了以下响应。
选项:

["test2", "我的新测试", "晚上住豪华房"]

defined_ids:

Object { 21::21="test2", 22::22="my new test", 24::24="晚上住豪华房"}

任何帮助都将不胜感激。提前致谢。

【问题讨论】:

  • 可能是服务器问题。你在萤火虫中看到了什么?你能给我们提琴吗?
  • 我在 firebug 中得到了这种响应, ["test2", "my new test", "night stay in delux room"] for res.options
  • 注册对象 { Zend_View_Helper_Doctype={...}, Zend_View_Helper_Placeholder_Registry={...}, db={...}, more...} path "/package/index" options [" test2”,“我的新测试”,“在豪华房过夜”] 0“test2”1“我的新测试”2“在豪华房过夜”defined_ids Object { 21::21="test2", 22:: 21="my new test", 24::24="晚上住豪华房"} 21::21 "test2" 22::21 "我的新测试" 24::24 "晚上住豪华房" .. ......完全响应。
  • IMO,["test2", "my new test", "night stay in delux room"] 是数组的格式而不是 json。
  • 应该是这样 "options" : { "test2","my new test", "night stay in delux room"} 让页面返回值,采用这种格式

标签: php jquery zend-framework autocomplete


【解决方案1】:

萤火虫显示的格式不是 JSON 格式。它是一个数组,使用索引访问。

显示输出时,请确保先json_encode() 数组,然后再显示它。

例如,关于问题,你的最终数组应该是这样的

$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);

接下来,请确保您的视图已关闭此请求。

【讨论】:

  • 响应应该在一个变量中返回。因此,当我执行 json_encode 时,我得到了以下无效的响应。选项 "["test2","我的新测试"]"
  • @BashantaDahal जी,您必须回显 json 编码变量。除非我看到更多数据和代码,否则我无能为力。
  • 感谢您的回复。我也需要defined_ids,因为我需要在每次请求后分配变量staticObject = res.defined_ids;这是服务器端的代码.. code
  • $keyword = $this->_getParam('keyword'); $elementDetailModel = new Package_Model_ElementDetail(); $arr = $elementDetailModel->searchElementDetail($keyword); //$arr['options'] 返回格式 array('test', 'my new test') //$arr['defined_ids'] 返回格式 array('2'=>'test', ' 23'=>'我的新测试') $this->view->options = $arr['options']; $this->view->defined_ids = $arr['defined_ids'];
【解决方案2】:

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

$ajaxContext = $this->_helper->getHelper('AjaxContext');
$ajaxContext->addActionContext('actionName', 'json')
            ->initContext();

并确保将 actionName 替换为您的操作控制器名称。

然后,$this->view->options = $arr['options'] 将自动转换为有效的 json 格式。

更多关于 AjaxContext 的信息here in the manual

【讨论】:

  • 感谢您的回复。我做了你提到的完全相同的事情。但是,它仍然无法正常工作。它在第一次尝试后起作用。我不知道为什么第一次尝试后自动完成功能不起作用
  • 很奇怪,我们需要有关您的问题的更多信息来帮助您(Firebug 说什么?)。另外,您是否考虑过使用ZendX_JQuery? (ZendX_JQuery_Form_Element_AutoComplete)
  • 我已经编辑了这个问题。希望你能对我的问题有更多的了解。
猜你喜欢
  • 1970-01-01
  • 2014-06-12
  • 1970-01-01
  • 2016-10-17
  • 2012-05-18
  • 2021-02-28
  • 2012-11-02
  • 1970-01-01
  • 2015-06-08
相关资源
最近更新 更多