2015-07-21 18 views
0

我有一個JQuery的級聯下拉,我從互聯網幾年前的工作正常。我想擴展它,以便在會話中記住團隊選擇。頁面上的級聯下載更改負載

前提是有一個團隊中的用戶,當您選擇一個團隊時,它將顯示該團隊中的活躍用戶。如果您在加載頁面後選擇了團隊,這將起作用,但如果設置了會話,則下拉不會級聯,您必須重新選擇團隊才能級聯。

$("select#memberid_active").attr("disabled", "disabled"); 
$("select#teamid_active").change(function() { 
    $("select#memberid_active").attr("disabled", "disabled"); 
    $("select#memberid_active").html("<option>Wait...</option>"); 
    var id = $("select#teamid_active option:selected").attr('value'); 
    $.post("selectconsultant_active.php", { 
     id : id 
    }, function(data) { 
     $("select#memberid_active").removeAttr("disabled"); 
     $("select#memberid_active").html(data); 
    }); 
}); 

這是下拉代碼,我有PHP會話工作,但當頁面加載時,級聯下拉不會更新。該功能必須適用於頁面加載時和更改時間。我嘗試了一些不同的方法,但我真的不明白JQuery足以讓它們工作。

任何幫助,將不勝感激。

感謝

回答

1

$("document").ready(function(){ 
 
    
 
/*This works to see if the active Option 
 
    is valid by default when loaded from session 
 
    and then fire the update method. As the default selected value for a dropdown is -1*/ 
 
    if($("select#teamid_active").val() != -1){  
 
    //Fire the udpate if the team dropdown has a value on DOC ready 
 
     updateTheUsersDropDown();  
 
    } 
 

 

 
}); 
 

 
$("select#memberid_active").attr("disabled", "disabled"); 
 
$("select#teamid_active").change(function() { 
 
    updateTheUsersDropDown(); 
 
}); 
 

 
var updateTheUsersDropDown=function(){ 
 
$("select#memberid_active").attr("disabled", "disabled"); 
 
    $("select#memberid_active").html("<option>Wait...</option>"); 
 
    var id = $("select#teamid_active option:selected").attr('value'); 
 
    $.post("selectconsultant_active.php", { 
 
     id : id 
 
    }, function(data) { 
 
     $("select#memberid_active").removeAttr("disabled"); 
 
     $("select#memberid_active").html(data); 
 
    }); 
 
};
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>

請嘗試這個曾經

+0

這似乎讓我真的非常接近,但如果沒有會話值設置不起作用。用戶下拉菜單是空白的。如果我將新功能和舊功能組合在一起,這很奇怪,它會按預期工作。 <選擇的id = 「teamid_active」 NAME = 「teamid_active」> <期權價值= 「」>選擇... 我猜如果($( 「#選擇teamid_active」)。VAL()! = -1)應該是if($(「select#teamid_active」)。val()!=「」)? –

+0

@ M_Jones88是這樣的事情,因爲我不知道確切的標記。這是給你一個背景,你如何解決這個問題。你可以請標記答案 –

+0

真棒,節省了我的時間.. !!!! –