dropdownlist jquery ddl.change()似乎在发生更改之前触发

dropdownlist jquery ddl.change()似乎在发生更改之前触发,jquery,asp.net-mvc,drop-down-menu,Jquery,Asp.net Mvc,Drop Down Menu,我有三个级联下拉列表 DllillnessType、DllillnessSubtype和DllInfectivityAgent 最初,仅显示DDillnessType。更改时,DDillnessSubtype将填充json数据并使其可见。到目前为止,一切顺利 接下来,当用户从DDillnessSubtype中选择一个值时,将在DDillnessSutType更改事件处理程序中为DDLLInfectivityAgent运行一个类似的过程。但是,在以下代码中,$(this).val()始终显示为“未

我有三个级联下拉列表

DllillnessType、DllillnessSubtype和DllInfectivityAgent

最初,仅显示DDillnessType。更改时,DDillnessSubtype将填充json数据并使其可见。到目前为止,一切顺利

接下来,当用户从DDillnessSubtype中选择一个值时,将在DDillnessSutType更改事件处理程序中为DDLLInfectivityAgent运行一个类似的过程。但是,在以下代码中,$(this).val()始终显示为“未定义”,即使用户选择了现有值:

$("#ddlIllnessSubType").change(function() {

var selection = $(this).val();

// go and get Json based on the value of selection.  
// Doesn't work cos selection == 'undefined'
var url = "/IllnessDetail/CascadedDdlInfectiousAgent/" + selection;

getJSON(url, function(data) {...});
...
});
value="undefined"
为什么选择==“未定义”

提前谢谢

安德鲁

PS:完整的jQuery和HTML如下所示:

完整的jQuery代码是:

<script type="text/javascript">

    $('document').ready(function() {
        var pEst = $("#pEncephalitisSubType");
        var pIa = $("#pInfectiousAgent");
        var ddlEst = $("#IdEncephalitisSubType");
        var ddlIa = $("#IdInfectiousAgent");

        $('#SubTypeContainer').hide();

        pEst.hide();
        pIa.hide();
//        debugger
        $('select').each(function() {
            $(this).val(""); //alert($(this).val()); 
        });

        // Change Event Handler
        $("#IdEncephalitisType").change(function() {
            var selection = $(this).val();
            pEst.fadeOut('slow');
            pIa.fadeOut('slow');
            ddlEst.val("");
            ddlIa.val("");

            if (selection == 0) {
                $('#SubTypeContainer').fadeOut('slow');
            }
            else {
                var url = "/Members/IllnessDetail/CascadedDdlSubType/" + selection;
                AjaxEncephalitisSubTypes(url);
            }
        });

        // Change Event Handler
        $("#IdEncephalitisSubType").change(function() {
            var selection = $(this).val();
            debugger
            pIa.fadeOut('slow');
            ddlIa.val("");
            if (selection != "") {
                if (($("#IdEncephalitisType").val() == 1) && ((selection == 1) || (selection == 2))) {
                    var url = "/Members/IllnessDetail/CascadedDdlInfectiousAgent/" + selection;
                    AjaxInfectiousAgents(url);
                }
            }
        });

        function AjaxEncephalitisSubTypes(url) {
            $('#SubTypeContainer').fadeOut('slow');
            $.getJSON(url, null, function(json) {
                ddlEst.empty();
                ddlIa.empty();
                PrependDdlDefaults(ddlEst);
                var i = 0;
                $.each(json, function(index, optionData) {
                    ddlEst.append("<option value='"
                        + optionData.EncephalitisSubTypeId
                        + "'>" + optionData.Name
                        + "</option>");
                    i++;
                });
                $('#SubTypeContainer').fadeIn('slow');
                ddlEst.val("");
                pEst.fadeIn('slow');
            });
        }

        function AjaxInfectiousAgents(url) {
            $('#SubTypeContainer').fadeOut('slow');
            $.getJSON(url, null, function(data) {
                var i = 0;
                ddlIa.empty();
                PrependDdlDefaults(ddlIa);
                $.each(data, function(index, optionData) {
                    ddlIa.append(
                    "<option value='"
                    + optionData.InfectiousAgentId
                    + "'>" + optionData.Name
                    + "</option>");
                    i++;
                });
            });
            ddlIa.val("");
            $('#SubTypeContainer').fadeIn('slow');
            pIa.fadeIn('slow');
        }

        function PrependDdlDefaults(ddl) {
            ddl.prepend(
                "<option value='"
                + ""
                + "'><i>" + " --- Please choose... --- "
                + "</i></option>");
        }
    });


</script>


<fieldset>
    <legend>The Illness</legend>
    <p>
        According to your input, different drop down lists will appear, specialised to only
        show the options that apply.
    </p>
    <p>
        <label for="IdEncephalitisType">
            Type Of Encephalitis:</label>
        <%= Html.DropDownList("IdEncephalitisType", Model.EncephalitisTypes)%>
        <%= Html.ValidationMessage("IdEncephalitisType", "*") %>
    </p>
    <div id="SubTypeContainer" style="margin-left:10px;border: solid 1px #ccc; background: #efefef;">
        <p class="highlight" id="lblSubTypeContainer" style="font-weight:bold;color:#c00;">
            Extra Information regarding the Illness:</p>
        <p id="pEncephalitisSubType">
            <label id="lblEncephalitisSubType" for="IdEncephalitisSubType">
                Sub Type of Encephalitis:</label>
            <%= Html.DropDownList("IdEncephalitisSubType", Model.EncephalitisSubTypes)%>
            <%= Html.ValidationMessage("IdEncephalitisSubType", "*") %>
        </p>
        <p id="pInfectiousAgent">
            <label id="lblInfectiousAgent" for="IdInfectiousAgent">
                Infectious Agent:</label>
            <%= Html.DropDownList("IdInfectiousAgent", Model.InfectiousAgents) %>
            <%= Html.ValidationMessage("IdInfectiousAgent", "*") %>
        </p>
    </div>
</fieldset>

$('document').ready(函数(){
var pEst=$(“#pEncephalitisSubType”);
var pIa=$(“PINFECIOUSAGENT”);
var ddlEst=$(“#idencephalitisubtype”);
变量ddlIa=$(“#IDifactiousAgent”);
$(“#子容器”).hide();
pEst.hide();
pIa.hide();
//调试器
$('select')。每个(函数(){
$(this.val(“”;//警报($(this.val());
});
//更改事件处理程序
$(“#idencephalitype”).change(函数(){
var selection=$(this.val();
害虫消失(“缓慢”);
pIa.fadeOut(“慢”);
ddlEst.val(“”);
ddlIa.val(“”);
如果(选择==0){
$(“#子容器”).fadeOut('slow');
}
否则{
var url=“/Members/IllnessDetail/cascadedldsubtype/”+选择;
ajax病毒亚型(url);
}
});
//更改事件处理程序
$(“#idencephalitisubtype”).change(函数(){
var selection=$(this.val();
调试器
pIa.fadeOut(“慢”);
ddlIa.val(“”);
如果(选择!=“”){
if($(“#idencephalitype”).val()=1)和&((selection==1)| |(selection==2))){
var url=“/Members/illnessdail/cascadeddlinfectiviousagent/”+选择;
Ajaxinfective代理(url);
}
}
});
函数子类型(url){
$(“#子容器”).fadeOut('slow');
$.getJSON(url,null,函数(json){
ddlEst.empty();
ddlIa.empty();
prependdldldlddefaults(ddlEst);
var i=0;
$.each(json、函数(索引、optionData){
ddlEst.append(“+optionData.Name
+ "");
i++;
});
$(“#子容器”).fadeIn('slow');
ddlEst.val(“”);
pEst.fadeIn(“慢”);
});
}
函数AjaxInfectiousAgents(url){
$(“#子容器”).fadeOut('slow');
$.getJSON(url,null,函数(数据){
var i=0;
ddlIa.empty();
PrependdlDefaults(ddlIa);
$.each(数据、函数(索引、选项数据){
ddlIa.append(
“”+optionData.Name
+ "");
i++;
});
});
ddlIa.val(“”);
$(“#子容器”).fadeIn('slow');
pIa.fadeIn(“慢”);
}
函数PrependdlDefaults(ddl){
ddl.prepend(
“+”---请选择…--”
+ "");
}
});
疾病

根据您的输入,将出现不同的下拉列表,专门针对
显示适用的选项。

脑炎类型:

脑炎亚型:

传染源:

不需要显示控制器,因为交付的json是正确的。就像在中一样,我已经测试了它,得到的渲染是正确的。

破解了它

问题出在json回调中:

$.each(json, function(index, optionData) {                    ddlEst.append("<option value='"                        + optionData.Id                        + "'>" + optionData.Name                        + "</option>");                    i++;                });
$.each(json, function(index, optionData) {
                    ddlEst.append("<option value='"
                        + optionData.Id
                        + "'>" + optionData.Name
                        + "</option>");
                    i++;
                });
aaaaaaaa rrrrrrr ggggggjjhhhhh