2013-10-08 30 views
1

我想顯示一個基於前面選擇的下拉列表。這是行不通的。它給了我列出的所有值。 ticket_fields_21114185是我的第一個下拉列表,其中包含[桌面,網頁瀏覽器]等值。 ticket_fields_21108325是我的第二個下拉列表,其中列出了所有其他值。使用jQuery顯示動態下拉列表,基於選定的值

$j(document).ready(function() { 
    var ren 
    var dtop = ['windows_7']; 
    var webrow= ['all','chrome','ie']; 

    //this builds the dropdown list 
    Buildoptions = {  
    selected: function(value, name){  
     return '<option value="'+value+'" selected="selected">'+name+'</option>';  
    },  
    notselected: function(value, name){  
     return '<option value="'+value+'">'+name+'</option>'; 
    },  
    clear: function(){  
     return '<option value=""></option>'; 
    } 
    } 
    //goes through the selected array and builds the options 
    var makeSelection = function(theField, theArray) {  
    for (x = 0; x < theArray.length; x++) {    
    console.log('makeing select ' + theArray)   
    if (x == 0)   
     $j(''+ theField +'').append(Buildoptions.notselected(theArray[x], theArray[x]));    

    } 
    } 

    $j('select#ticket_fields_21114185').change(function(){ 

    ren=$j('select#ticket_fields_21114185 option:selected').val(); 

    if(ren=='desktop'){ 
     makeSelection('select#ticket_fields_21108325',dtop); 
    } 
    else 
     makeSelection('select#ticket_fields_21108325',webrow) 
    }); 
}); 

回答

0

也許這只不過是你最後的else語句沒有括號{}
可能會觸發這兩個選項時,桌面選擇
跳水在你仔細看看代碼現在,如果這是不是它不給你的希望呢! :)



編輯:在查看您的代碼後,我有更多的問題,然後回答。
爲了讓事情有點輕鬆,你可以粘貼你的html以及(儘管我認爲我可以處理2個選擇)。
如果我理解正確,你的第二選擇應該顯示一些動態數據取決於第一選擇。
我做了以下結論:

  • 您的代碼缺少一些;每次發言之後在這裏和那裏。

  • 在添加新函數之前,您還沒有明確的功能來刪除動態選擇的值 。

  • 你對數組的循環對我來說很奇怪,爲什麼是x + 2而不是+1?它看起來像代碼想要使用數組中的第一個值作爲值,第二個作爲名稱。但你的名單隻有3個值不是6?

是否有可能複製粘貼代碼,並以現在它是增加值和不確定的動態選擇後靜態的每個選擇改變選擇?

+0

對選擇功能做了少量更改,現在可以使用。代碼包含更改。謝謝 –

+0

沒問題,快樂編碼! – Kristof

相關問題