如何为自动完成功能获取正确的 json 数据类型?
当我尝试此操作时,它按预期工作:在两个字符之后,它显示匹配的条目。
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<script src="development-bundle/jquery-1.6.2.js"></script>
<script src="development-bundle/ui/jquery.ui.core.js"></script>
<script src="development-bundle/ui/jquery.ui.widget.js"></script>
<script src="development-bundle/ui/jquery.ui.position.js"></script>
<script src="development-bundle/ui/jquery.ui.autocomplete.js"></script>
<script type="text/javascript">
$( document ).ready( function() {
var data = [ 'John', 'Jack', 'Joe', 'Lisa', 'Barbara' ];
$( "#name" ).autocomplete({
source: data,
minLength: 2
});
});
</script>
</head>
<body>
<form>
<table>
<tr><td>Name:</td><td><input type="text"
id="name" name="name" /></td></tr>
</table><br />
<input type="submit" value="OK"/>
</form>
</body>
</html>
这表现得不同:在两个字符之后它总是显示所有条目?
第二个例子有什么问题?
#!/usr/local/bin/perl
use warnings;
use 5.014;
use utf8;
use Mojolicious::Lite;
get '/eingabe' => sub {
my $self = shift;
$self->render( 'eingabe' );
};
get '/search_db' => sub {
my $self = shift;
$self->render( json => [ 'John', 'Jack', 'Joe', 'Lisa', 'Barbara' ] );
};
app->start;
__DATA__
@@ eingabe.html.ep
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<script src="/development-bundle/jquery-1.6.2.js"></script>
<script src="/development-bundle/ui/jquery.ui.core.js"></script>
<script src="/development-bundle/ui/jquery.ui.widget.js"></script>
<script src="/development-bundle/ui/jquery.ui.position.js"></script>
<script src="/development-bundle/ui/jquery.ui.autocomplete.js"></script>
<script type="text/javascript">
$( document ).ready( function() {
$( "#name" ).autocomplete({
source: '/search_db',
minLength: 2
});
});
</script>
</head>
<body>
<form>
<table>
<tr><td>Name:</td><td><input type="text"
id="name" name="name" /></td></tr>
</table><br />
<input type="submit" value="OK"/>
</form>
</body>
</html>
When I try this, it works as expected: after two characters its shows the matching entries.
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<script src="development-bundle/jquery-1.6.2.js"></script>
<script src="development-bundle/ui/jquery.ui.core.js"></script>
<script src="development-bundle/ui/jquery.ui.widget.js"></script>
<script src="development-bundle/ui/jquery.ui.position.js"></script>
<script src="development-bundle/ui/jquery.ui.autocomplete.js"></script>
<script type="text/javascript">
$( document ).ready( function() {
var data = [ 'John', 'Jack', 'Joe', 'Lisa', 'Barbara' ];
$( "#name" ).autocomplete({
source: data,
minLength: 2
});
});
</script>
</head>
<body>
<form>
<table>
<tr><td>Name:</td><td><input type="text"
id="name" name="name" /></td></tr>
</table><br />
<input type="submit" value="OK"/>
</form>
</body>
</html>
This behaves different: after two characters it shows always all entries?
What is wrong with the second example?
#!/usr/local/bin/perl
use warnings;
use 5.014;
use utf8;
use Mojolicious::Lite;
get '/eingabe' => sub {
my $self = shift;
$self->render( 'eingabe' );
};
get '/search_db' => sub {
my $self = shift;
$self->render( json => [ 'John', 'Jack', 'Joe', 'Lisa', 'Barbara' ] );
};
app->start;
__DATA__
@@ eingabe.html.ep
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<script src="/development-bundle/jquery-1.6.2.js"></script>
<script src="/development-bundle/ui/jquery.ui.core.js"></script>
<script src="/development-bundle/ui/jquery.ui.widget.js"></script>
<script src="/development-bundle/ui/jquery.ui.position.js"></script>
<script src="/development-bundle/ui/jquery.ui.autocomplete.js"></script>
<script type="text/javascript">
$( document ).ready( function() {
$( "#name" ).autocomplete({
source: '/search_db',
minLength: 2
});
});
</script>
</head>
<body>
<form>
<table>
<tr><td>Name:</td><td><input type="text"
id="name" name="name" /></td></tr>
</table><br />
<input type="submit" value="OK"/>
</form>
</body>
</html>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在第一个示例中,您使用数组来设置自动完成的选项。在第二个中,数组被序列化为 json,但是自动完成功能期望 json 具有特定的键/值对(id、标签和值)。
我认为最好的选择是为自动完成数据定义自定义回调,如 来自 nettuts 的本教程。
您的自动完成代码将类似于以下内容:
In your first example you are using an array to set the options of the autocomplete. In the second the array is being searilizied into json, however the autocomplete expects the json to have specific key/value pairs (id, label, and value).
I think your best bet would be to define a custom callback for the autocomplete's data as shown in this tutorial from nettuts.
Your autocomplete code would look something similar to this:
我认为你的例子没有任何问题,它运行得很好。
但请确保您从正确的路径加载 JS,并且尝试使用正确的 url: http://127.0.0.1 :3000/eingabe
我修改了你的示例来加载 Google 托管的库,它确实有效:
https://gist.github.com/106e8c4eb7483333aa08
(至少在 Chrome 和 Firefox 中)
I think nothing is wrong wrong with your example, it works perfectly.
But please make sure you are loading your JS from right path and that you are trying right url: http://127.0.0.1:3000/eingabe
I modified your example to load Google hosted libraries and it's just works:
https://gist.github.com/106e8c4eb7483333aa08
(at least in Chrome and Firefox)