【发布时间】: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