如何使用Jquery Ajax调用填充下拉菜单?

时间:2020-12-23 09:47:50

I have a WebMethod which gets data that I want to fill DropDown with in a DataSet. Currently I am filling the dropdown using a hardcoded object. But I want to replace this hard coded object with data returned by webmethod.

我有一个WebMethod,它获取我想在数据集中填充下拉菜单的数据。目前,我正在使用硬编码对象填充下拉菜单。但是我想用webmethod返回的数据替换这个硬编码的对象。

 [System.Web.Services.WebMethod]
         public static string GetDropDownDataWM(string name)
         {
             //return "Hello " + name + Environment.NewLine + "The Current Time is: "
             //    + DateTime.Now.ToString();

             var msg = "arbaaz";

             string[] name1 = new string[1];
             string[] Value = new string[1];
             name1[0] = "@Empcode";
             Value[0] = HttpContext.Current.Session["LoginUser"].ToString().Trim();
             DataSet ds = new DataSet();
             dboperation dbo = new dboperation();
             ds = dbo.executeProcedure("GetDropDownsForVendor", name1, Value, 1);

             return ds.GetXml(); 

         }

CLIENT SIDE(UPDATE 1):

客户端(更新1):

  <script type = "text/javascript">
    function GetDropDownData() {
        var myDropDownList = $('.myDropDownLisTId');

        $.ajax({
            type: "POST",
            url: "test.aspx/GetDropDownDataWM",
            data: '{name: "abc" }',
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (data) {
                $.each(jQuery.parseJSON(data.d), function () {
                    myDropDownList.append($("<option></option>").val(this['FieldDescription']).html(this['FieldCode']));
                });
            },
            failure: function (response) {
                alert(response.d);
            }
        });
    }
    function OnSuccess(response) {
        console.log(response.d);
        alert(response.d);
    }
</script>

3 个解决方案

#1


11  

function GetDropDownData() {
    $.ajax({
        type: "POST",
        url: "test.aspx/GetDropDownDataWM",
        data: '{name: "abc" }',
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(data.d)
                {
                    $.each(data.d, function (){
                        $(".myDropDownLisTId").append($("<option     />").val(this.KeyName).text(this.ValueName));
                    });
                },
        failure: function () {
            alert("Failed!");
        }
    });
}

#2


1  

From the WebMethod, don't send DataSet directly, send XML...

从WebMethod中,不要直接发送数据集,发送XML…

[System.Web.Services.WebMethod]
public static string GetDropDownDataWM(string name)
{
    DataSet ds = new DataSet();
    ds.Tables.Add("Table0");
    ds.Tables[0].Columns.Add("OptionValue");
    ds.Tables[0].Columns.Add("OptionText");
    ds.Tables[0].Rows.Add("0", "test 0");
    ds.Tables[0].Rows.Add("1", "test 1");
    ds.Tables[0].Rows.Add("2", "test 2");
    ds.Tables[0].Rows.Add("3", "test 3");
    ds.Tables[0].Rows.Add("4", "test 4");

    return ds.GetXml();
}

Before Ajax call...

Ajax调用之前……

var myDropDownList = $('.myDropDownLisTId');

Try like below...(inside Ajax call)

试着像下面…(在Ajax调用)

success: function (response) {
    debugger;

    $(response.d).find('Table0').each(function () {
           var OptionValue = $(this).find('OptionValue').text();
           var OptionText = $(this).find('OptionText').text();
           var option = $("<option>" + OptionText + "</option>");
           option.attr("value", OptionValue);

           myDropDownList.append(option);
     });
},

Note:

注意:

  1. OptionValue and OptionText are the Columns of DataSet Table.

    选项值和选项文本是数据集表的列。

  2. $(response.d).find('Table0').each(function (){}) - Here Table0 is the name of Table inside DataSet.

    $(response.d);(“Table0”)。每个(function(){}) -这里的表0是数据集中表的名称。

#3


0  

 var theDropDown = document.getElementById("myDropDownLisTId");
                theDropDown.length = 0;
                $.each(items, function (key, value) {

                    $("#myDropDownLisTId").append($("<option></option>").val(value.PKId).html(value.SubDesc));

here "SubDesc",PKId describes the value getting out of Database., u need to separate your value from dataset.

在“SubDesc”中,PKId描述了从数据库中获取的值。,你需要将你的值与数据集分开。

#1


11  

function GetDropDownData() {
    $.ajax({
        type: "POST",
        url: "test.aspx/GetDropDownDataWM",
        data: '{name: "abc" }',
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(data.d)
                {
                    $.each(data.d, function (){
                        $(".myDropDownLisTId").append($("<option     />").val(this.KeyName).text(this.ValueName));
                    });
                },
        failure: function () {
            alert("Failed!");
        }
    });
}

#2


1  

From the WebMethod, don't send DataSet directly, send XML...

从WebMethod中,不要直接发送数据集,发送XML…

[System.Web.Services.WebMethod]
public static string GetDropDownDataWM(string name)
{
    DataSet ds = new DataSet();
    ds.Tables.Add("Table0");
    ds.Tables[0].Columns.Add("OptionValue");
    ds.Tables[0].Columns.Add("OptionText");
    ds.Tables[0].Rows.Add("0", "test 0");
    ds.Tables[0].Rows.Add("1", "test 1");
    ds.Tables[0].Rows.Add("2", "test 2");
    ds.Tables[0].Rows.Add("3", "test 3");
    ds.Tables[0].Rows.Add("4", "test 4");

    return ds.GetXml();
}

Before Ajax call...

Ajax调用之前……

var myDropDownList = $('.myDropDownLisTId');

Try like below...(inside Ajax call)

试着像下面…(在Ajax调用)

success: function (response) {
    debugger;

    $(response.d).find('Table0').each(function () {
           var OptionValue = $(this).find('OptionValue').text();
           var OptionText = $(this).find('OptionText').text();
           var option = $("<option>" + OptionText + "</option>");
           option.attr("value", OptionValue);

           myDropDownList.append(option);
     });
},

Note:

注意:

  1. OptionValue and OptionText are the Columns of DataSet Table.

    选项值和选项文本是数据集表的列。

  2. $(response.d).find('Table0').each(function (){}) - Here Table0 is the name of Table inside DataSet.

    $(response.d);(“Table0”)。每个(function(){}) -这里的表0是数据集中表的名称。

#3


0  

 var theDropDown = document.getElementById("myDropDownLisTId");
                theDropDown.length = 0;
                $.each(items, function (key, value) {

                    $("#myDropDownLisTId").append($("<option></option>").val(value.PKId).html(value.SubDesc));

here "SubDesc",PKId describes the value getting out of Database., u need to separate your value from dataset.

在“SubDesc”中,PKId描述了从数据库中获取的值。,你需要将你的值与数据集分开。