【问题标题】:jQuery Multiselect auto selectjQuery 多选自动选择
【发布时间】:2012-06-25 03:52:34
【问题描述】:

我在某个时候有这个工作,因为我过去有一个关于这个的问题对我有帮助,但现在我丢失了代码,所以我回到我的问题并试图让它工作,现在它是不...

请有人帮我看看为什么这不再起作用了?

代码

var valArr = [3, 4];
size = valArr.length;
for (i = 0; i < size; i++) {
    $("#secondary_group option[value='" + valArr[i] + "']").
                                            attr("selected", 1);
    $("#secondary_group").multiselect("refresh");
}

演示: http://jsfiddle.net/VXbLE/

它应该做的是根据它的值选择选项,基本上,你可以从jsfiddle中读取代码并弄清楚。

我是 JavaScript/jQuery 的初学者,所以我不完全理解它......

【问题讨论】:

    标签: jquery multi-select


    【解决方案1】:
    var valArr = [3, 4];
    size = valArr.length; // detect array length
    // looping over array
    for (i = 0; i < size; i++) {
    
        // $("#secondary_group option[value='" + valArr[i] + "']")
        // select the option with value match with the valArr
        // from the select with id=secondary_group and if match found
        // .attr("selected", 1);  make that option as default selected
    
        $("#secondary_group option[value='" + valArr[i] + "']")
                                                .attr("selected", 1);
    }
    
    // after selecting the options
    // refresh the select using below code
    // And this code should belong outside of
    // above loop, because
    // refreshing within loop will only 
    // select last matched element
    // not all matched
    
    $("#secondary_group").multiselect("refresh");
    

    DEMO

    【讨论】:

    • 哦,所以我的问题是我告诉它在循环中刷新?
    • @unlucky4ever 是的,刷新应该在循环之外
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-08-07
    • 1970-01-01
    • 2023-03-26
    • 2021-10-29
    • 1970-01-01
    • 2012-05-05
    • 1970-01-01
    相关资源
    最近更新 更多