jquery 自动完成选项不起作用
我正在使用 jquery 自动完成
<script src='http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js'></script>
<script src='http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.2/jquery-ui.min.js'></script>
一切工作正常,但现在我尝试添加一个不执行任何操作的 minChars 选项。我的代码如下所示:
$( "#searchid" ).autocomplete({
source: "/autocomplete_url.php?more=1",
minChars:6,
select: function(event, ui) {
$("#searchid").val(ui.item.value);
$("#formid").submit();
}
});
一切正常,除了它忽略我添加的选项。为什么/如何调试这个问题?
更新:根据下面的答案,我已改用 minLength 。我尝试过以两种不同的方式添加,但它不起作用:/这是我的“新”代码:
$( ".selector" ).autocomplete({ minLength: 6 });
$( "#searchid" ).autocomplete({
source: "/autocomplete_url.php?more=1",
minLength:6,
select: function(event, ui) {
event.preventDefault();
$("#searchid").val(ui.item.value);
$("#formid").submit();
}
});
谢谢
I'm using jquery autocomplete
<script src='http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js'></script>
<script src='http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.2/jquery-ui.min.js'></script>
Everything is working Ok., but now I'm trying to add a minChars option which doesn't do anything. My code looks like this:
$( "#searchid" ).autocomplete({
source: "/autocomplete_url.php?more=1",
minChars:6,
select: function(event, ui) {
$("#searchid").val(ui.item.value);
$("#formid").submit();
}
});
Everything is working, except it disregards the options I'm adding. Why/How can I debug the problem?
UPDATE: according to the answer below, I've moved to use minLength instead. I've tried adding in two different ways but it doesn't work :/ This is my "new" code:
$( ".selector" ).autocomplete({ minLength: 6 });
$( "#searchid" ).autocomplete({
source: "/autocomplete_url.php?more=1",
minLength:6,
select: function(event, ui) {
event.preventDefault();
$("#searchid").val(ui.item.value);
$("#formid").submit();
}
});
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
它不是
minChars
而是minLength
请参阅http://jqueryui.com/demos/autocomplete/#option-minLength
It's not
minChars
butminLength
see http://jqueryui.com/demos/autocomplete/#option-minLength
一般来说,如果您想操作自动完成本身所附加的表单元素,您应该
在您的
select: function(event, ui){}
块中包含一个。否则,默认的自动完成行为将与您的代码冲突,因为它也尝试设置该值。Generally, if you want to manipulate the form element that the autocomplete itself is attached to, you should include a
inside your
select: function(event, ui){}
block. Otherwise the default autocomplete behaviour will conflict with your code, as it's also trying to set that value.确保您的源返回带有键的 JSON:id、value 和 label。如果没有,您将需要使用回调函数作为源。查看这篇关于 nettus 的文章:
http://net.tutsplus.com/tutorials/javascript-ajax/how-to-use-the-jquery-ui-autocomplete-widget/
Make sure that your source returns JSON with the keys: id, value, and label. If it doesn't you'll need to use a callback function as your source. Check out this article on nettus:
http://net.tutsplus.com/tutorials/javascript-ajax/how-to-use-the-jquery-ui-autocomplete-widget/