2
我正在製作一個網站,我需要將數據添加到從數據庫中獲取數據的C#類的下拉列表中。如何將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> <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>
我曾嘗試使用以下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) {
}
});
}
我錯過了什麼?
它只是增加了更多的dropwonlists - 現在從C#正確的數據,但不是我的文本框,並刪除按鈕 – Oedum
@Oedum看到更新的答案 –