jquery 自动完成选项不起作用

发布于 2024-12-04 04:53:35 字数 1094 浏览 2 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

小忆控 2024-12-11 04:53:35

它不是 minChars 而是 minLength
请参阅http://jqueryui.com/demos/autocomplete/#option-minLength

$( ".selector" ).autocomplete({ minLength: 0 });

It's not minChars but minLength
see http://jqueryui.com/demos/autocomplete/#option-minLength

$( ".selector" ).autocomplete({ minLength: 0 });
晨与橙与城 2024-12-11 04:53:35

一般来说,如果您想操作自动完成本身所附加的表单元素,您应该

event.preventDefault();

在您的 select: function(event, ui){} 块中包含一个。否则,默认的自动完成行为将与您的代码冲突,因为它也尝试设置该值。

Generally, if you want to manipulate the form element that the autocomplete itself is attached to, you should include a

event.preventDefault();

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.

╰ゝ天使的微笑 2024-12-11 04:53:35

确保您的源返回带有键的 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/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文