代码之家  ›  专栏  ›  技术社区  ›  awrigley

DropDownList jQuery ddl.change()似乎在发生更改之前激发。

  •  1
  • awrigley  · 技术社区  · 16 年前

    我有三个级联下拉列表。

    ddillness类型、ddillness子类型和ddl感染剂

    最初,只显示ddillnessType。更改时,ddillnessSubtype将填充JSON数据并使其可见。到目前为止,一切都很好。

    接下来,当用户从ddillnessSuttype更改事件处理程序中选择一个值时,将为ddillnessSuttype更改事件处理程序中的ddlInfluencousAgent运行类似的过程。但是,在以下代码中,$(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) {...});
    ...
    });
    

    为什么选择='未定义'?你说什么?你说什么?你说什么?

    提前谢谢

    安得烈

    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>
    

    不需要显示控制器,因为传递的JSON是正确的。就像我测试过的那样,得到的结果是正确的。

    1 回复  |  直到 16 年前
        1
  •  2
  •   awrigley    16 年前

    破解它!!!!!!!!

    问题出在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++;
                    });
    

    …where optiondata.id不是字段的正确名称!噢,**amp;^%$_“!γ

    因此,所有标记都具有以下值:

    value="undefined"
    

    aaaaaaaa rrrrrr ggggg jjjhhhh!!!!!!!!!!!