[英]how fill the dynamically created dropdownlist with data from database using ajax and web service in asp.net c#
var index = 0;
function newitem() {
index++;
var objTo = document.getElementById('newitem')
var divtest = document.createElement("div");
divtest.setAttribute("class", "form-group removeclass" + index);
var rdiv = 'removeclass' + index;
divtest.innerHTML = '<div class="col-sm-3 nopadding"><div class="form-group">
<select class="form-control" id="ddlitem' + index + '"name="ddlitem"><option
value="">--select--</select> </div></div><div class="col-sm-3
nopadding"><div class="form-group"> <input type="text"
class="form-control" id="txtquantity' + index + '"
name="txtquantity" value=""></div></div><div class="col-sm-3
nopadding"><div class="form-group"> <input type="text"
class="form-control" id="txtprice' + index + ' "name="txtprice"
disabled="disabled" value="" ></div></div><div class="col-sm-3
nopadding"><div class="form-group"><div class="input-group"> <input
type="text" class="form-control" id="txttotal' + index + '"
name="txttotal" disabled="disabled" value="" ><div
class="input-group-btn"> <button class="btn btn-danger"
type="button" onclick="remove_created_item(' + index + ');"> <span
class="glyphicon glyphicon-minus" aria-hidden="true"></span>
</button></div></div></div></div><div class="clear"></div>';
objTo.appendChild(divtest);
};
function remove_created_item(idOfCreatedItem) {
$('.removeclass' + idOfCreatedItem).remove();
}
太亂了,但我想我知道您在做什么。
在objTo.appendChild(divtest);
將您select
下拉列表插入頁面,對API進行Ajax調用:
var copyOfIndex = index;
$.get( "/api/whatever", function( data ) {
$( "#ddlitem" + copyOfIndex ).html( data ); // data => can be the html containing all the <options>
alert( "Load was performed." );
});
不確定上面的代碼是如何初始化的,但是您應該使用索引的副本(此處為copyOfIndex
)來確保ajax調用完成時ajax調用未使用過時的index
值。
另外,看起來您需要先關閉對象,然后再通過objTo.appendChild(divtest);
將元素插入DOM中objTo.appendChild(divtest);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.