gpt4 book ai didi

jquery - Ajax 调用通过单个请求绑定(bind)多个 HTML 控件

转载 作者:行者123 更新时间:2023-12-01 01:31:21 25 4
gpt4 key购买 nike

我正在尝试使用 Ajax 调用绑定(bind)多个 HTML 选择控件,但经过我的所有努力,我的过程失败了。这里出了什么问题?

这是返回五个数据表的 Web 服务:

[WebMethod]
public List<List<ListItem>> DealerLoad(string otype, string target, string para)
{
ds = AL.ExecuteDataSet("sp_dlr_NewDealer_query", CommandType.StoredProcedure, new[]
{
new MySqlParameter("OType", otype),
new MySqlParameter("Target", target),
new MySqlParameter("id", 0)
});
List<ListItem> list1 = new List<ListItem>();
List<ListItem> list2 = new List<ListItem>();
List<ListItem> list3 = new List<ListItem>();
List<ListItem> list4 = new List<ListItem>();
List<ListItem> list5 = new List<ListItem>();

List<ListItem> list = new List<ListItem>();
for (int i = 0; i < ds.Tables.Count; i++) {
foreach (DataRow dr in ds.Tables[i].Rows)
{

list.Add(new ListItem
{
Value = dr[0].ToString(),
Text = dr[1].ToString()
});

if (i == 0)
{
list1 = list;
}
else if (i == 1)
{
list2 = list;
}
else if (i == 2)
{
list3 = list;
}
else if (i == 3)
{
list4 = list;
}
else if (i == 4)
{
list5 = list;
}
}
}
return new List<List<ListItem>>{list1, list2, list3, list4, list5};
}

这是 jQuery Ajax 调用:

var url = '../WebDeal.asmx/DealerLoad2';
var data = {};
var control = $('#ddl_country').attr('id') + ',' +
$('#ddl_countryallow').attr('id') + ',' +
$('#ddl_center').attr('id') + ',' +
$('#ddl_Superior').attr('id') + ',' +
$('#ddl_SuperExe').attr('id');
data.otype = 'select';
data.target = 'GetLoadData';
data.para = '';
BindControl(url, JSON.stringify(data), control);

绑定(bind)控制方法

函数 BindControl(xurl, xdata, xcontrol) {

$.ajax({
type: "POST",
data: xdata,
url: xurl,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (result) {
onSuccess1(result, xcontrol);
},
error: function (error) {
alert(error.responseText);
}
});

};

调用成功

function onSuccess1(responese, control) {

var chk = control;
var controlarr = String(chk).split(',');
if (controlarr.length == 1) {
control.empty().append('<option value=0 selected="selected">Select</option>');
if (responese.d != null) {
$.each(responese.d, function () {
control.append($("<option></option>").val(this['Value']).html(this['Text']));
});
}
hideload();
}
else {
$.each(controlarr, function (index, values) {
var controls = $('#' + values);

controls.append('<option value=0 selected="selected">Select</option>');
$.each(responese.d[index], function () {
controls.append($("<option></option>").val(this['Value']).html(this['Text']));
hideload();
});
});
}
};

最佳答案

[WebMethod]
public List<List<ListItem>> DealerLoad(string otype, string target, string para)
{
ds = AL.ExecuteDataSet("sp_dlr_NewDealer_query", CommandType.StoredProcedure, new[]
{
new MySqlParameter("OType", otype),
new MySqlParameter("Target", target),
new MySqlParameter("id", 0)
});
List<ListItem> list1 = new List<ListItem>();
List<ListItem> list2 = new List<ListItem>();
List<ListItem> list3 = new List<ListItem>();
List<ListItem> list4 = new List<ListItem>();
List<ListItem> list5 = new List<ListItem>();

List<ListItem> list = new List<ListItem>();
for (int i = 0; i < ds.Tables.Count; i++) {
foreach (DataRow dr in ds.Tables[i].Rows)
{

list.Add(new ListItem
{
Value = dr[0].ToString(),
Text = dr[1].ToString()
});

if (i == 0)
{
list1 = list;
}
else if (i == 1)
{
list2 = list;
}
else if (i == 2)
{
list3 = list;
}
else if (i == 3)
{
list4 = list;
}
else if (i == 4)
{
list5 = list;
}
}
}



// *Please try to bind the dropdown here rather than clientside*

return new List<List<ListItem>>{list1, list2, list3, list4, list5};
}

关于jquery - Ajax 调用通过单个请求绑定(bind)多个 HTML 控件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50061649/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com