简体   繁体   English

页面加载时的级联下拉列表更改

[英]Cascading Dropdown change on load of page

I have a Jquery Cascading drop down I took from the Internet some years ago which works fine. 我有一个几年前从互联网上获取的Jquery Cascading下拉列表,它运行良好。 I want to expand it though so that the team selection is remembered in a Session. 但我想扩展它,以便在会议中记住团队的选择。

The premise is there are users in a team, when you select a team it will display active users in that team. 前提是团队中有用户,当您选择一个团队时,它将显示该团队中的活动用户。 This works if you select the team once you load the page but if the Session is set the drop down doesn't cascade and you have to reselect the team for it to then cascade. 如果您在加载页面后选择了小组,但是如果设置了会话,则下拉列表不会级联,并且您必须重新选择小组以进行级联,这将起作用。

$("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);
    });
});

This is the Drop Down code and I have the PHP Session working but when the page is loaded the cascading drop down does not update. 这是下拉代码,我可以使用PHP会话,但是在加载页面时,级联下拉列表不会更新。 The function would have to work for when the page loads and when you change. 该功能必须在页面加载和更改时起作用。 I've tried a few different approaches but I really don't understand JQuery enough to make them work. 我尝试了几种不同的方法,但实际上我对JQuery的了解还不足以使其正常工作。

Any help would be appreciated. 任何帮助,将不胜感激。

Thanks 谢谢

 $("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> 

Please try this once 请尝试一次

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM