我正在创建级联下拉列表,但出现错误:


    

function GetCity(_warehousesId) {
    var procemessage = "<option value='0'> Please wait...</option>";
    $("#ddlcity").html(procemessage).show();
    var url = "/Carriage/GetCityByStaeId";

    $.ajax({
        url: url,
        data: { warehousesid: _warehousesId },
        cache: false,
        type: "POST",
        success: function (data) {
            var markup = "<option value='0'>Select City</option>";
            for (var x = 0; x < data.length; x++) {
                markup += "<option value=" + data[x].Value + ">" + data[x].Text + "</option>";
            }
            $("#ddlcity").html(markup).show();
        },
        error: function (reponse) {
            alert("error : " + reponse);
        }
    });

};




 @using (Html.BeginForm())
{
  @Html.DropDownListFor(m => m.StateModel, new  SelectList(Model.StateModel, "WarehousesID", "Name"), new { @id = "ddlstate", @style = "width:200px;", @onchange = "javascript:GetCity(this.value);" })
<br />
<br />
<select id="ddlcity" name="ddlcity" style="width: 200px"></select>

<br /><br />
}


而且,我总是得到一个错误:“未定义GetCity”。我究竟做错了什么?

最佳答案

您的(function GetCity(_warehousesId) {})(jQuery)是错误的。您尝试创建自执行功能,然后再尝试使用它。

它应该只是function GetCity(_warhouseId) {}以便以后能够执行。



如果<script>具有src属性,则其中的任何内容都将被忽略。为jQuery和您的代码使用不同的标签:

 <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js"></script>
 <script type="text/javascript">
     function GetCity(_warehousesId) {}
 </script>

关于javascript - jQuery GetCity未定义,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43340215/

10-09 23:08