如何将数据从C#添加到动态添加的下拉列表

时间:2015-06-04 07:54:36

标签: javascript c# jquery asp.net

我正在创建一个网站,我需要将数据添加到C#类的下拉列表中,该类从数据库中获取数据。

我创建了一个网站,我可以使用jquery动态添加下拉列表和文本框:

$(document).ready(function () {
        var max_fields = 10; //maximum input boxes allowed
        var wrapper = $(".input_fields_wrap"); //Fields wrapper
        var add_button = $(".add_field_button"); //Add button ID

        var x = 1; //initlal text box count
        $(add_button).click(function (e) { //on add input button click
            e.preventDefault();
            if (x < max_fields) { //max input box allowed
                x++; //text box increment

                $(wrapper).append('<div><select id="ddlCon" >getCars()</select>&nbsp;&nbsp;&nbsp;&nbsp;<input type="text" name="mytext[]"/><a href="#" class="remove_field">Remove</a></div>'); //add input box
            }
        });

        $(wrapper).on("click", ".remove_field", function (e) { //user click on remove text
            e.preventDefault(); $(this).parent('div').remove(); x--;
        })
    });

<div class="input_fields_wrap">
        <button class="add_field_button">Add More Fields</button>
        <div>
            <select id="ddlCon"><option></option></select>&nbsp;&nbsp;&nbsp;&nbsp;<input type="text" name="mytext[]" />
        </div>
    </div>

我尝试过使用以下jquery,但我无法使用它:

 function getCars() {
        $.ajax({
            type: "POST",
            url: "GS1-128-Palle.aspx/populatePalleList",
            data: "{'index':1}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (response) {
                var branches = response.d;
                $('select').empty();
                $.each(branch, function (index, branches) {
                    $('select').append('<option >' + branch + '</option>');
                });
            },

            failure: function (msg) {
            }
        });
    }

我错过了什么?

2 个答案:

答案 0 :(得分:2)

试试这个: -

$(add_button).click(function (e) { //on add input button click
e.preventDefault();
if (x < max_fields) { //max input box allowed
    x++; //text box increment
    var dv = $('<div/>'),
     select = $('<select/>').appendTo(dv);
    dv.append('<input type="text" name="mytext[]"/><a href="#" class="remove_field">Remove</a>');
    $(wrapper).append(dv);
    getCars(select);
 }
});

和你的getCars功能: -

function getCars(select) {
$.ajax({
    type: "POST",
    url: "GS1-128-Palle.aspx/populatePalleList",
    data: "{'index':1}",
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function (response) {
        var branches = response.d;
        $(select).empty();
        $.each(branches, function (index, branch) {
            $(select).append('<option >' + branch + '</option>');
        });
    },

    failure: function (msg) {
    }
});
 }

答案 1 :(得分:0)

可能这可以帮到你。

 success: function (result) {
                        var markup = "";
                        if (result.length < 1) {
                            markup = "<option value='0'>--Nothing to Select--</option>";
                        } else {
                            data = result;
                            markup = "<option value='0'>--Select--</option>";
                            for (var x = 0; x < data.length; x++) {
                                markup += "<option value=" + data[x].ValueId + ">" + data[x].DisplayText + "</option>";
                            }
                        }
                        $("#Dropdownid").html(markup).show();

                    }