jquery 多选重新加载

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8653301/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 10:50:44  来源:igfitidea点击:

jquery multiselect reload

jqueryjquery-ui

提问by Robin Michael Poothurai

How can I add or remove options in JQuery UI Multiselect? I am initializing the multiselect on page load and I need to remove existing values and add new values based on another selection. I am initializing the multiselect on page load using:

如何在JQuery UI Multiselect 中添加或删除选项?我正在页面加载时初始化多选,我需要删除现有值并根据另一个选择添加新值。我正在使用以下方法在页面加载时初始化多选:

$("#multipleselectboxId").multiselect();

After that, I am adding values to the multiple drop down using jQuery append()and remove()methods which are working fine with original dropdown but in the multiselect are not getting reflected.

之后,我使用 jQueryappend()remove()方法向多个下拉列表添加值,这些方法在原始下拉列表中工作正常,但在多选中没有得到反映。

Can anyone help with this?

有人能帮忙吗?

回答by Brandon Joyce

For that one you could just destroy and reinitialize after changing...

对于那个,您可以在更改后销毁并重新初始化...

$("#multipleselectboxId").append(toAppend).multiselect("destroy").multiselect();

There's also another plugin with a refresh function: EricHynds's Multiselect

还有另一个具有刷新功能的插件:EricHynds 的 Multiselect

$("#multipleselectboxId").append(toAppend).multiselect("refresh");

回答by Robin Michael Poothurai

I found the solution for this, first destroy the multiselect and reInitialize it, Thanks for @ Brandon Joyce,

我找到了解决方案,首先销毁多选并重新初始化它,感谢@ Brandon Joyce,

solutions for this is

对此的解决方案是

$("#multipleselectboxId").append(toAppend);
$("#multipleselectboxId").remove(toRemove);

$("#multipleselectboxId").multiselect('destroy');
$("#multipleselectboxId").multiselect();

回答by Andrej Gaspar

I was trying to rebuild multiselect by .multiselect("destroy")and .multiselect(), but it wasnt working, so finally I find this solution worked for me.

我试图通过.multiselect("destroy")and重建多选.multiselect(),但它不起作用,所以最后我发现这个解决方案对我有用。

$.each(jsonArray, function(i, val) {
    $('#frmarticles-f_category_id').append('<option value="'+i+'">'+val+'</option>').multiselect('rebuild');
});

回答by Vinit

Thank you this helped. I was using multiselect UI widget and this is what worked

谢谢这有帮助。我正在使用多选 UI 小部件,这就是有效的

jQuery("select[title='" + FieldNameTitleText + "']").append( "<option value='" + OptionValue+ "'>" + OptionText + "</option>" ).multiselect("refresh");

回答by user2139254

In my case, I just wanted to replace all previous content of multiselect with new.

就我而言,我只想用 new 替换多选的所有先前内容。

This worked for me:

这对我有用:

$('#multiselect_id').html(newHtml);
$('#multiselect_id').multiselect('refresh');

回答by UncaAlby

Here is what I did:; it may be more than necessary, but it worked for me.

这是我所做的:; 这可能是不必要的,但它对我有用。

Original "select" code that requires modification:

需要修改的原始“选择”代码:

<select id="MySelect" name="selection">
<option value="1">One</option>
<option value="2">Two</option>
</select>

I rebuild the option list in PHP, send it to the JavaScript via JSON, and construct/store the new list in a variable. E.g.:

我在 PHP 中重建选项列表,通过 JSON 将其发送到 JavaScript,然后在变量中构造/存储新列表。例如:

// this is similar to if we got it from PHP
var newList = '<option value="A">Alpha</option>
<option value="B">Beta</option>
<option value="C">Gamma</option>';

Now, to switch this around in the jQuery UI Multiselect widget:

现在,要在 jQuery UI Multiselect 小部件中进行切换:

$('#MySelect').html(''); // clear out old list
$('#MySelect').multiselect('destroy');  // tell widget to clear itself
$('#MySelect').html(newList); // add in the new list
$('#MySelect').multiselect(); // re-initialize the widget

In particular, I re-initialized it with parameters, e.g.:

特别是,我用参数重新初始化了它,例如:

$('#MySelect').multiselect({selectedList: 4, header: false});

If anybody has read this far down and is still having troubles, give this a try.

如果有人读了这么远但仍然遇到问题,请试一试。

回答by Yoong Kim

well this plugin works fine but I have a pb with destroy and filter: my combo data are loaded with ajax. So, when I refresh the data with ajax call, I call destroy to refresh the plugin:

这个插件很好用,但我有一个带销毁和过滤器的 pb:我的组合数据是用 ajax 加载的。所以,当我用ajax调用刷新数据时,我调用destroy来刷新插件:

myCombo.multiselect('destroy');
myCombo.multiselect().multiselectfilter();

It works for the first call: empty combo, ajax call to load data, call the functions above. But if I refresh the combo data and call again the above functions, the filter disappeared? Someone experienced this pb before and found a solution?

它适用于第一次调用:空组合,ajax 调用加载数据,调用上面的函数。但是如果我刷新组合数据并再次调用上述函数,过滤器消失了?之前有人经历过这个pb并找到了解决方案?

回答by Ercan

I used it in this way :

我以这种方式使用它:

$("#<%= cmbInBayiID.ClientID %>").multiselect().trigger('reset');

It worked.

有效。

回答by Mykola Mykolayovich Dolynskyi

For wenzhixin MultiSelect:

对于文之心 MultiSelect

var i = jQuery('input');
i.data('multipleSelect').$parent.remove();
i.removeData('multipleSelect');
i.show();

回答by Anderson Petry

    function setMultiSelect(idElement, paramVal){
        eval("$('#"+idElement+"').multiselect('uncheckAll')");
        $.each($('input[name="multiselect_'+idElement+'"]'), function(k,i) {    
            if(paramVal.indexOf(this.value)!=-1){
                this.checked = true; 
            }
        }); 
         eval("$('#"+idElement+"').multiselect('update')");
    }