jQuery 自动完成和 JSON 响应 - 手动提交与自动提交时的不同响应
我在这个 jQuery UI 自动完成设置中看到了奇怪的行为。
当我开始输入“Smith”时,自动完成功能会在下拉菜单中提供多个选项(例如“Joe Smith”、“Mary Taylor”、“Jack Sparrow”)。在控制台上,我没有看到任何错误,响应为
[{"value":"Joe Smith"},{"value":"Mary Taylor"},{"value":"Jack Sparrow"}]
但是,如果我点击提交/搜索按钮,那么我会得到一个空白页面:
[{"value":"Joe Smith"}]
不知何故,我的模型查询在运行时返回所有用户jQuery 自动完成——但是当我手动触发它时,它会返回正确的结果。
知道这里出了什么问题吗?
谢谢。
JS:
$(function() {
function log( message ) {
$( "<div/>" ).text( message ).prependTo( "#log" );
$( "#log" ).attr( "scrollTop", 0 );
}
$( "#search_input" ).autocomplete({
source: "http://example.com/search",
minLength: 2,
select: function( event, ui ) {
log( ui.item ?
"Selected: " + ui.item.value + " aka " + ui.item.id :
"Nothing selected, input was " + this.value );
}
});
});
控制器(search.php,CodeIgniter 标记):
function index()
{
$term = $this->input->post('search_input');
$response = json_encode($this->search_model->search($term));
echo $response;
}
模型(search_model.php,CodeIgniter 标记):
function search($term)
{
$query = $this->db->query("
SELECT up.first_name, up.last_name
FROM user_profiles up, users u, pets p
WHERE u.activated = 1
AND u.banned = 0
AND up.last_name LIKE '%" . $term . "%'
GROUP BY up.last_name
ORDER BY up.last_name ASC;
");
$search_data = array();
foreach ($query->result() as $row) {
$search_data[] = array(
'value' => $row->first_name . ' ' . $row->last_name,
);
}
return $search_data;
}
I am seeing strange behavior in this jQuery UI autocomplete setup.
When I begin typing say "Smith", the autocomplete offers several options in a dropdown (eg, "Joe Smith", "Mary Taylor", "Jack Sparrow"). On the console I see no errors and the response is
[{"value":"Joe Smith"},{"value":"Mary Taylor"},{"value":"Jack Sparrow"}]
But if I hit the submit/search button, then I get a blank page with:
[{"value":"Joe Smith"}]
Somehow, my Model query returns all users when running through jQuery autocomplete -- but when I trigger it manually it returns the correct result.
Any idea what's wrong here?
Thanks.
JS:
$(function() {
function log( message ) {
$( "<div/>" ).text( message ).prependTo( "#log" );
$( "#log" ).attr( "scrollTop", 0 );
}
$( "#search_input" ).autocomplete({
source: "http://example.com/search",
minLength: 2,
select: function( event, ui ) {
log( ui.item ?
"Selected: " + ui.item.value + " aka " + ui.item.id :
"Nothing selected, input was " + this.value );
}
});
});
Controller (search.php, CodeIgniter markup):
function index()
{
$term = $this->input->post('search_input');
$response = json_encode($this->search_model->search($term));
echo $response;
}
Model (search_model.php, CodeIgniter markup):
function search($term)
{
$query = $this->db->query("
SELECT up.first_name, up.last_name
FROM user_profiles up, users u, pets p
WHERE u.activated = 1
AND u.banned = 0
AND up.last_name LIKE '%" . $term . "%'
GROUP BY up.last_name
ORDER BY up.last_name ASC;
");
$search_data = array();
foreach ($query->result() as $row) {
$search_data[] = array(
'value' => $row->first_name . ' ' . $row->last_name,
);
}
return $search_data;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您似乎没有发送搜索词。我已将其简化为一个 php 函数。 $term 将由自动完成脚本发送过来。
It looks like you are not sending over the search term. I've simplified it to one php function. $term Is going to be sent over by the autocomplete script.
我认为更好的解决方案是使用 jQuery .ajax() 并将函数设置为 POST 数据。这样我就可以避免使用
GET
并且不必创建额外的控制器来处理POST
和GET
。I think a better solution is to use
jQuery .ajax()
and set the function toPOST
data. That way I can avoid usingGET
and don't have to create an additional controller to handle bothPOST
andGET
.