jquery
  • multi-select
  • 2012-06-25 85 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

    相關問題