【问题标题】:I found an Autocomplete plugin on the web but I do not understand how to implement it in my template我在网上找到了一个自动完成插件,但我不明白如何在我的模板中实现它
【发布时间】:2019-06-21 14:14:02
【问题描述】:

你能帮我用这个插件吗:
https://www.jqueryscript.net/form/Bootstrap-Combo-Box-Typeahead-Plugin-jQuery.html

我正在使用 Bootstrap 3,并且我已经将插件放在我的静态文件夹中。

我尝试在我的选择字段中添加一个 id,但没有成功。

forms.py

class IndividuForm(forms.Form):
    individu = forms.ModelChoiceField(queryset=Individu.objects.order_by('nom'), empty_label="")

    def __init__(self, *args, **kwargs):
        super(IndividuForm, self).__init__(*args, **kwargs)
        self.fields['individu'].widget.attrs={'class': 'combobox','id':'mySelect'}

脚本

<script>
   $('.mySelect').combobox()
</script>

【问题讨论】:

    标签: javascript jquery html django django-forms


    【解决方案1】:

    据我所知,您要定位的领域是:

    <select class="combobox" id="mySelect"></select>
    

    但是你的 jQuery 是针对的

    <select class="mySelect"></select>
    

    , 要定位正确的元素,您应该使用:

    $('#mySelect').combobox()
    

    还要确保在页面上的组合框之前调用 jQuery。

    【讨论】:

    • 感谢您的回复,我尝试了您所说的但 bootstrap-combobox.js 未加载到我的应用程序中。我不明白为什么。
    • 确保&lt;script src="bootstrap-combobox.js"&gt;&lt;/script&gt; 包含在您页面的&lt;head&gt; 中。您可以检查该文件是否包含在浏览器的开发工具中(在 Chrome 中的 sources 和 Firefox 中的 Debugger 下)
    • 我放了 href 而不是 src,现在文件已加载,但出现错误 ReferenceError: $ is not defined
    • @Zapoux 确保在组合框之前包含 jQuery,一切都应该正常工作。
    • 感谢您的帮助,我解决了我的问题。事实上,这些文件的顺序不正确,最重要的是,我忘记在包含我的文件之前写 {% static ... %}。
    【解决方案2】:

    对我来说,我以前从未听说过组合框,我可能年纪大了:)

    但是你需要做的很简单。 只需在 jquery 中给变量一个函数

    $('#select').on('change', function() {
      alert( "Do whatever u want");
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-05-28
      • 2016-06-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多