javascript - How to add data from C# to dynamically added dropdownlist -
i making site need add data dropdownlist c# class data database.
i have made site can dynamically add dropdownlist , text box using 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> <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> <input type="text" name="mytext[]" /> </div> </div>
i have tried using following jquery, can't work:
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) { } }); }
what missing?
try this:-
$(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); } });
and getcars function:-
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) { } }); }
Comments
Post a Comment