小编典典

从AJAX填充的下拉列表中获取价值

ajax

我已经用AJAX填充了ASP.net下拉列表,现在我需要获取ID以C#方法存储到数据库中((我正在使用LINQ)

这是我的网络方法

[WebMethod]
public static ArrayList GetLanguageList()
{
    ArrayList lstArrLanguage = new ArrayList();
     IQueryable<Common.Town> myList = new SupplierBL().GetTowns();
     foreach(Common.Town t in myList)
    {
        string name = t.Name;
        string id = t.TownId.ToString();
        lstArrLanguage.Add(new ListItem(name, id));
    }

    return lstArrLanguage;
}

我的test.aspx代码

<script language="javascript" type="text/javascript">
    $(document).ready(function () {
        $.ajax({
            type: "POST",
            url: "test.aspx/GetLanguageList",
            data: '',
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (msg) {
                $("#ddlLanguage").empty().append($("<option></option>").val("[-]").html("Please select"));
                $.each(msg.d, function () {
                     $('#<%=ddlLanguage.ClientID%>').append($("<option></option>").val(this['Value']).html(this['Text']));
                });
            },
            error: function () {
                alert("An error has occurred during processing your request.");
            }
        });
    });
</script>

阅读 214

收藏
2020-07-26

共1个答案

小编典典

DropDownList如果在中添加选项,则无法从中获取所选值javaScript。您可以尝试以下

string selectedValue = Request.Form[ddlLanguage.UniqueID];
2020-07-26