jquery
  • multi-select
  • 2012-06-25 45 views 1 likes 
    1

    我曾在某个时间点工作过,因为我在过去帮助过我,但现在我失去了代码,所以我回到我的问题,并试图让它工作,现在它不是...jQuery Multiselect自动选择

    请有人帮我,为什么这不工作了?

    CODE

    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的初学者,所以我不明白它完全...

    回答

    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

    +0

    哦,所以我的问题是,我告诉它在循环刷新? – unlucky4ever

    +0

    @ unlucky4ever yup,刷新应该在循环之外 – thecodeparadox

    +0

    好的,谢谢,我会记住对未来:)再次感谢! – unlucky4ever

    相关问题