简体   繁体   English

在Django中,bootstrap-multiselect对Javascript是否正确?

[英]Is Javascript correct with bootstrap-multiselect in Django?

I'm trying to create a multiselect dropdown menu witht the bootstrap-multiselect plugin in Django 1.4.5. 我正在尝试在Django 1.4.5中创建一个带有bootstrap-multiselect插件的multiselect下拉菜单。 Link to plugin: http://davidstutz.github.io/bootstrap-multiselect/ 链接到插件: http : //davidstutz.github.io/bootstrap-multiselect/

My form.py is: 我的form.py是:

weight_training_days = forms.MultipleChoiceField(
    help_text=u'(Required) Select 3 days',
    widget=forms.SelectMultiple(attrs={
        'inline': True,
        'class': 'multiselect',
    }),
    choices=(
        (0, "Mon"),
        (1, "Tue"),
        (2, "Wed"),
        (3, "Thu"),
        (4, "Fri"),
        (5, "Sat"),
        (6, "Sun"),
    ),
)

My form in the template is: 我在模板中的表单是:

<form class="form-{{ layout }}" action="" method="post">
    {% csrf_token %}

    {{ form|as_bootstrap:layout }}

    <p class="form-actions">
        <input type="submit" value="Submit Changes" class="btn btn-primary">
    </p>
</form>

And the js code in the bootstrap-multiselect.js file (which is linked to in the static folder): bootstrap-multiselect.js文件(链接到静态文件夹中)中的js代码:

 $(document).ready(function() {
   $('.multiselect').multiselect({
     buttonClass: 'btn',
     buttonWidth: 'auto',
     buttonContainer: '<div class="btn-group" />',
     maxHeight: false,
     buttonText: function(options) {
       if (options.length == 0) {
         return 'None selected <b class="caret"></b>';
       }
       else if (options.length > 3) {
         return options.length + ' selected  <b class="caret"></b>';
       }
       else {
         var selected = '';
         options.each(function() {
           selected += $(this).text() + ', ';
         });
         return selected.substr(0, selected.length -2) + ' <b class="caret"></b>';
       }
     }
   });
 });

For some reason the plugin is not working. 由于某种原因,插件无法正常工作。 Only a standard SelectMultiple field is presented. 仅显示一个标准的SelectMultiple字段。

I have very limited experience with javascript and am not sure why this plugin won't work. 我在javascript方面的经验非常有限,并且不确定为什么该插件无法正常工作。 Any feedback/assistance is greatly appreciated. 任何反馈/帮助,我们将不胜感激。 Thanks 谢谢

The issue was due to my inexperience/knowledge of javascript. 该问题是由于我对JavaScript缺乏经验/知识所致。 I need to download the library for this plugin and that is what went in the bootstrap-multiselect.js file. 我需要下载该插件的库,而这正是bootstrap-multiselect.js文件中的内容。 Works great now. 现在效果很好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM