【问题标题】:Boostrap Multiselect condition if checked如果选中,则引导多选条件
【发布时间】:2018-09-25 17:49:18
【问题描述】:

我有一个包含值的数组,我提出一个请求,将信息发送到多选。

当我打开我的选择时,我预先选择了几个值。

我的问题是,如果我单击取消选中 boostrap 多选上的复选框,我想从我的数组中删除此信息。如果我单击复选框,我想将值放回数组中。

在我的代码中,我首先检查值是否在数组中,但与 boostrap 选择的交互不是我想要做的。

    // that bring the information in boostrap multiselect
    var vetornovo = ValueChecked();

    // here i try to detect the change to check and unchecked but doesnt works
    // all informations is selected: "selected" by default
    $("#os-select-list").change(function() {
                // i take the checkbox value.
                var valueck = parseInt($("#os-select-list option:selected").text());

                // if the information is in that i array when i click to unchecked then remove.
                  if (vetornovo.indexOf(valueck) > -1) {
                        index = $.inArray(valueck, vetornovo);
                        vetornovo.splice(index, 1);  
                        console.log(vetornovo);     
                    // when i click to selecte the value again i push and insert back to the array.
                  } else {
                    vetornovo.push(valueck);
                    console.log(vetornovo);   
                  }
    });

我想了解什么是错的,为什么当我点击复选框时不做并且有时会重复内容。

【问题讨论】:

    标签: javascript jquery multi-select bootstrap-multiselect


    【解决方案1】:

    假设您正在使用 https://www.jqueryscript.net/form/jQuery-Multiple-Select-Plugin-For-Bootstrap-Bootstrap-Multiselect.html

    检测变化的命令是

    * Triggered on change of the multiselect.
    *
    * Not triggered when selecting/deselecting options manually.
    *
    * @param {jQuery} option
    * @param {Boolean} checked 
    */
    onChange : function(option, checked) {
    
    }
    

    不像你在代码中那样“改变”

    有许多“选择”可以与引导程序一起使用 - 我喜欢 https://developer.snapappointments.com/bootstrap-select/ - 它有很好的示例和易于阅读的说明 - 如果您刚刚开始,值得一看...

    【讨论】:

    • onChange : function(option, checked) { } 对我不起作用,因为我的多选结构在 HTML 中,而代码在 .js 中。我会尝试将多选传输到 .js 文件。 $('#os-select-list').multiselect({ enableFiltering: true, includeSelectAllOption: true, nonSelectedText: '', allSelectedText: '.', maxHeight: 400, enableClickableOptGroups : true });
    • 你应该像davidstutz.de/bootstrap-multiselect/…一样构建你的代码(是的,在javascript中...... - 就像你的例子所示......)。如果您需要更多帮助,您还需要显示您在 ValueChecked() 中的代码。我认为您只需要更好地配置您的逻辑,但我还没有全面了解。另外,我不明白你的意思是“多选结构在 HTML 中”——这是不可能的,因为它显然是 JS .....再次显示你的所有代码,你可以获得帮助。
    • 这个作品哈哈
    猜你喜欢
    • 2013-01-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-01
    • 2017-01-13
    相关资源
    最近更新 更多