我正在使用AddMoreDrugsToBilling方法动态创建选择框。并通过在selectbox的onclick事件中调用showDrugs来填充选项。问题是当我将selectbox作为select2(可搜索的选择框)时,选择框的onclick事件不再被调用,因此值不会被加载到选择框。如何将动态创建的选择框设置为select2并使用值填充它?
$(document).ready(function() {
debugger;
$(".selectDrugs").select2();
})
$(function() {
getDrugs();
AddMoreDrugsToBilling();
});
function AddMoreDrugsToBilling() {
//debugger;
if ($("#tbl_Drugs tbody").length > 0) {
$("<tr id=" + tblDrugsCount + "><td><select class='selectDrugs' width='250px' id='txt_Drugs" + (tblDrugsCount) + "' onchange='onChangeDrugText(this," + tblDrugsCount + ");' onclick='showDrugs(" + tblDrugsCount + ");' /></td><td><select id='txt_Batches" + (tblDrugsCount) + "' onchange='onChangeBatchText(this," + tblDrugsCount + ");' /></td><td><label id='lbl_ExpiryDate" + (tblDrugsCount) + "' /></td><td><label id='lbl_UnitPrice" + (tblDrugsCount) + "' type='text'/></td><td><label id='lbl_AvlQty" + (tblDrugsCount) + "' type='text'/></td><td><input class='form-control input-100px' onkeyup='CheckMaxQuantity(" + (tblDrugsCount) + ");' id='txt_Qty" + (tblDrugsCount) + "' type='text'/></td><td><label id='lbl_Amount" + (tblDrugsCount) + "' ></label></td><td><img src='../Images/delete.gif' id='img_delete" + (tblDrugsCount) + "' title='Delete' onclick='DeleteDrugItemrow(this);'/></td></tr>").appendTo("#tbl_Drugs tbody");
}
else {
$("<tbody><tr id=" + tblDrugsCount + "><td><select class='selectDrugs' width='250px' id='txt_Drugs" + (tblDrugsCount) + "' onchange='onChangeDrugText(this," + tblDrugsCount + ");' onclick='showDrugs(" + tblDrugsCount + ");' /></td><td><select id='txt_Batches" + (tblDrugsCount) + "' onchange='onChangeBatchText(this," + tblDrugsCount + ");' /></td><td><label id='lbl_ExpiryDate" + (tblDrugsCount) + "' /></td><td><label id='lbl_UnitPrice" + (tblDrugsCount) + "' type='text'/></td><td><label id='lbl_AvlQty" + (tblDrugsCount) + "' type='text'/></td><td><input class='form-control input-100px' onkeyup='CheckMaxQuantity(" + (tblDrugsCount) + ");' id='txt_Qty" + (tblDrugsCount) + "' type='text'/></td><td><label id='lbl_Amount" + (tblDrugsCount) + "' ></label></td> <td><img src='../Images/delete.gif' id='img_delete" + (tblDrugsCount) + "' title='Delete' onclick='DeleteDrugItemrow(this);'/></td></tr></tbody>").appendTo("#tbl_Drugs");
}
tblDrugsCount++;
}
var drugsData = [];
function getDrugs() {
// debugger;
jQuery.ajax({
type: 'POST',
contentType: 'application/json;charset=utf-8',
data: {},
url: 'NewPharmacyOrder.aspx/FillDrugs',
success: function(data) {
//debugger;
var resultDrugItems = data.d;
for (i = 0; i < resultDrugItems.length; i++) {
var item1 = resultDrugItems[i];
drugsData.push({ "DrugName": item1.DrugName, "DrugId": item1.DrugId});
}
}
});
}
function showDrugs(id) {
debugger;
var txtDrugsList = $("#txt_Drugs" + id);
var txtDrugsListCtrl = document.getElementById("txt_Drugs" + id);
//alert(txtDrugsListCtrl.length);
if (txtDrugsListCtrl.length == 0) {
//if ($(txtDrugsList).length == 1) {
// if($("#txt_Drugs"+id) option).length)
txtDrugsList.empty();
$("#txt_Drugs" + id).get(0).options[0] = new Option("select drug", "-1");
$.each(drugsData, function(index, item) {
// debugger;
var option1 = new Option(item.DrugName, item.DrugId);
//option1.setAttribute('data-availablebatches', item.AvailableBatches);
txtDrugsList.append(option1);
});
// }
}
}
答案 0 :(得分:0)
试试这个;)
您认为getDrugs();
调用将提取数据,然后控件将转到AddMoreDrugsToBilling();
,这不会发生,因为您正在进行异步发送的ajax请求。
Async:False
=代码已暂停。 (其他代码等待为此完成..)Async:True
=代码继续。 (没有任何内容被暂停。其他代码不等待。)就这么简单。
因此,为了确保加载数据,您只需将函数调用AddMoreDrugsToBilling();
移至success
回调,而不是仅在getDrugs();
之后调用
以下是更新的代码:
$(function(){
debugger;
$(".selectDrugs").select2();
getDrugs();
});
function AddMoreDrugsToBilling(){
//debugger;
if($("#tbl_Drugs tbody").length > 0){
$("<tr id=" + tblDrugsCount + "><td><select class='selectDrugs' width='250px' id='txt_Drugs" + (tblDrugsCount) + "' onchange='onChangeDrugText(this," + tblDrugsCount + ");' onclick='showDrugs(" + tblDrugsCount + ");' /></td><td><select id='txt_Batches" + (tblDrugsCount) + "' onchange='onChangeBatchText(this," + tblDrugsCount + ");' /></td><td><label id='lbl_ExpiryDate" + (tblDrugsCount) + "' /></td><td><label id='lbl_UnitPrice" + (tblDrugsCount) + "' type='text'/></td><td><label id='lbl_AvlQty" + (tblDrugsCount) + "' type='text'/></td><td><input class='form-control input-100px' onkeyup='CheckMaxQuantity(" + (tblDrugsCount) + ");' id='txt_Qty" + (tblDrugsCount) + "' type='text'/></td><td><label id='lbl_Amount" + (tblDrugsCount) + "' ></label></td><td><img src='../Images/delete.gif' id='img_delete" + (tblDrugsCount) + "' title='Delete' onclick='DeleteDrugItemrow(this);'/></td></tr>").appendTo("#tbl_Drugs tbody");
}else{
$("<tbody><tr id=" + tblDrugsCount + "><td><select class='selectDrugs' width='250px' id='txt_Drugs" + (tblDrugsCount) + "' onchange='onChangeDrugText(this," + tblDrugsCount + ");' onclick='showDrugs(" + tblDrugsCount + ");' /></td><td><select id='txt_Batches" + (tblDrugsCount) + "' onchange='onChangeBatchText(this," + tblDrugsCount + ");' /></td><td><label id='lbl_ExpiryDate" + (tblDrugsCount) + "' /></td><td><label id='lbl_UnitPrice" + (tblDrugsCount) + "' type='text'/></td><td><label id='lbl_AvlQty" + (tblDrugsCount) + "' type='text'/></td><td><input class='form-control input-100px' onkeyup='CheckMaxQuantity(" + (tblDrugsCount) + ");' id='txt_Qty" + (tblDrugsCount) + "' type='text'/></td><td><label id='lbl_Amount" + (tblDrugsCount) + "' ></label></td> <td><img src='../Images/delete.gif' id='img_delete" + (tblDrugsCount) + "' title='Delete' onclick='DeleteDrugItemrow(this);'/></td></tr></tbody>").appendTo("#tbl_Drugs");
}
tblDrugsCount++;
}
var drugsData = [];
function getDrugs(){
// debugger;
jQuery.ajax({
type: 'POST',
contentType: 'application/json;charset=utf-8',
data: {},
url: 'NewPharmacyOrder.aspx/FillDrugs',
success: function(data){
//debugger;
var resultDrugItems = data.d;
for(i = 0; i < resultDrugItems.length; i++){
var item1 = resultDrugItems[i];
drugsData.push({"DrugName": item1.DrugName,
"DrugId": item1.DrugId});
}
AddMoreDrugsToBilling(); /* here you can call this to appened data */
}
});
}
function showDrugs(id){
debugger;
var txtDrugsList = $("#txt_Drugs" + id);
var txtDrugsListCtrl = document.getElementById("txt_Drugs" + id);
//alert(txtDrugsListCtrl.length);
if(txtDrugsListCtrl.length == 0){
//if ($(txtDrugsList).length == 1) {
// if($("#txt_Drugs"+id) option).length)
txtDrugsList.empty();
$("#txt_Drugs" + id).get(0).options[0] = new Option("select drug", "-1");
$.each(drugsData, function(index, item){
// debugger;
var option1 = new Option(item.DrugName, item.DrugId);
//option1.setAttribute('data-availablebatches', item.AvailableBatches);
txtDrugsList.append(option1);
});
// }
}
}