嘿,我完全是一个新手,我正在寻找一种像谷歌一样拥有自动补全功能的搜索框。
我搜索过了,我找到的最好的潜在客户似乎是我在论坛上找到的http://www.pengoworks.com/workshop/jquery/autocomplete.htm。建议它的人说他在http://code.google.com/p/searchable-behaviour-for-cakephp/上使用它,这是完全正确的,因为我在最后一次尝试弄清楚cakephp时已经设法安装了可搜索。
问题是,我以前没有用过太多的javascript,我对我到底要做什么感到有点困惑。自动补全代码的文档并没有涉及到我能理解的任何细节。
如果我们假设我设法正确地安装了可搜索的行为,那么任何好心的人能向我解释如何让自动补全功能工作吗?
它说只需使用:
$("selector").autocomplete(url [, options]);例如:
$("#input_box").autocomplete("autocomplete_ajax.cfm");Autocomplete要求存在id为"input_box“的输入元素。当用户开始在输入框中键入内容时,自动补全程序将使用名为q的GET参数请求autocomplete_ajax.cfm
这就是我不明白的地方。我该把它放在哪里?一旦我把它放在某个地方,我是否只需要将其中一个输入命名为"input_box"?
提前谢谢。
发布于 2011-05-22 22:58:14
有三个步骤:
1)在视图中使用Html helper创建一个带有输入域的完全正常的表单:
// app/views/foo_bars/search.ctp
<?php
echo $this->Form->create('FooBar');
echo $this->Form->input('field');
echo $this->Form->end('Submit');
?>2)触发jquery自动完成:
// app/views/foo_bars/search.ctp
<?php
echo $this->Html->scriptBlock(
.'$("#FooBarField").autocomplete({'
.'source:"/foo_bars/find",'
.'delay: 100,'
.'select:function(event,ui){$(this).parent().parent().children("input").val(ui.item.id);},'
.'open:function(event,ui){$(this).parent().parent().children("input").val(0);}'
.'});'
array('inline' => false));
?>3)通过控制器查询数据库,得到可能的值:
// app/controllers/foo_bars_controller.php
<?php
public function find() {
if ($this->RequestHandler->isAjax()) {
$this->autoLayout = false;
$this->autoRender = false;
$this->FooBar->recursive = -1;
$results = $this->FooBar->find('all', array('fields' => array('id', 'name'), 'conditions' => array('name LIKE "%'.$_GET['term'].'%"')));
$response = array();
$i = 0;
foreach($results as $result){
$response[$i]['value'] = $result['FooBar']['name'];
$response[$i]['id'] = $result['FooBar']['id'];
$i++;
}
echo json_encode($response);
}
}
?>发布于 2013-06-18 23:34:43
echo $this->Html->scriptBlock(
'$("#FooBarField").autocomplete({‘
.'source:"/Search/find",'
.'delay: 100,'
.'select:function(event,ui){$(this).parent().parent().children("input").val(ui.item.id);},'
.'open:function(event,ui){$(this).parent().parent().children("input").val(0);}'.'});'.
array('inline' => false));https://stackoverflow.com/questions/6071828
复制相似问题