通过 jQuery.Ajax 的返回值附加到 DropDownList
我有2个DropDownList,就像主从一样。 这是我的 Default.aspx:
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table>
<tr>
<td>
<asp:Label ID="MsLbl" runat="server" Text="Groups" />
</td>
<td>
<asp:DropDownList ID="Masterddl" runat="server">
<asp:ListItem Text="G1" Value="G1" />
<asp:ListItem Text="G2" Value="G2" />
<asp:ListItem Text="G3" Value="G3" />
</asp:DropDownList>
</td>
</tr>
<tr>
<td>
<asp:Label ID="Svlbl" runat="server" Text="Members" />
</td>
<td>
<asp:DropDownList ID="Slaveddl" runat="server" />
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
这是我的脚本:
$(document).ready(function () {
$('select#Masterddl').change(function () {
MasterChangeHandler($(this).val());
});
function MasterChangeHandler(Value) {
$.ajax({
type: 'Post',
url: 'MasterSlaveHandler.ashx',
dataType: "text",
data: 'ItemSelected=' + Value,
async: 'true',
success: function (data) { SuccessHandler1(data); }
});
}
function SuccessHandler1(data) {
$('select#Slaveddl').empty();
$.each(data, function (i, slaveValue) {
$('select#Slaveddl').append(
$('<option></option>').val(slaveValue.Value).html(slaveValue.Text)
);
});
}
和我的处理程序:
public class SlaveValues {
public string Value { get; set; }
public string Text { get; set; }
}
public class MasterSlaveDropDownListsHandler : IHttpHandler {
public bool IsReusable {
get { return true; }
}
public void ProcessRequest(HttpContext context) {
string valueSelected = context.Request["ItemSelected"];
List<SlaveValues> slaveValues = new List<SlaveValues>();
SlaveValues sv;
sv = new SlaveValues();
sv.Text = "SV1";
sv.Value = valueSelected + "s1";
slaveValues.Add(sv);
sv = new SlaveValues();
sv.Text = "SV2";
sv.Value = valueSelected + "s2";
slaveValues.Add(sv);
string responseText =
Newtonsoft.Json.JsonConvert.SerializeObject(slaveValues);
context.Response.ContentType = "text/json";
context.Response.Write(responseText);
}
}
但没有任何内容可以附加。 另外,我在 Firebug 窗口中看到如下响应(当我从 Master ddl 中选择 G2 时):
[{"Value":"G2s1","Text":"SV1"},{"Value":"G2s2","Text":"SV2"}]
最后,我用这个新脚本更改了脚本的成功方法进行测试:
function SuccessHandler2(data) {
$('select#Slaveddl').empty();
$.each(data, function (i, slaveValue) {
$('select#Slaveddl').append(
$('<option></option>').val('Member' + i).html('Member' + i)
);
});
}
当尝试这个新脚本时,与 Slave ddl 的绑定工作正常,但使用一些附加项目:索引显示member0到member30,我不知道为什么。
编辑1: 我也尝试了这个并正确附加:
function SuccessHandler3(data) {
var values = [{ "Value": "G2s1", "Text": "SV1" }, { "Value": "G2s2", "Text": "SV2"}];
$('select#Slaveddl').empty();
$.each(values, function (i, slaveValue) {
$('select#Slaveddl').append(
$('<option></option>').val('Member' + slaveValue.Value).html('Member' +
slaveValue.Text)
);
});
}
所以我认为返回值(数据)的操作存在问题。
对于更具体的视图,下图是当我在 Master ddl 中选择 G3 时 Firebug 窗口中的 JSON 选项卡:
Edit2:
我也尝试这个,只出现第一个警报,显然 (data.d) 为空或未知对象:
function SuccessHandler6(data) {
var selection = $('select#Slaveddl');
$(selection).children().remove();
alert('in handler and remove children correctly');
if (data.d) {
alert('data.d is not null');
$(data.d).each(function (index, item) {
$(selection).append('<option>').val(item.Value).html(item.Text);
alert('after append in index: ' + index);
});
}
}
如何正确使用返回值并附加到 Slave ddl?问题出在哪里?
I have 2 DropDownList, like Master-Slave.
This is my Default.aspx:
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table>
<tr>
<td>
<asp:Label ID="MsLbl" runat="server" Text="Groups" />
</td>
<td>
<asp:DropDownList ID="Masterddl" runat="server">
<asp:ListItem Text="G1" Value="G1" />
<asp:ListItem Text="G2" Value="G2" />
<asp:ListItem Text="G3" Value="G3" />
</asp:DropDownList>
</td>
</tr>
<tr>
<td>
<asp:Label ID="Svlbl" runat="server" Text="Members" />
</td>
<td>
<asp:DropDownList ID="Slaveddl" runat="server" />
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
And this is my Script:
$(document).ready(function () {
$('select#Masterddl').change(function () {
MasterChangeHandler($(this).val());
});
function MasterChangeHandler(Value) {
$.ajax({
type: 'Post',
url: 'MasterSlaveHandler.ashx',
dataType: "text",
data: 'ItemSelected=' + Value,
async: 'true',
success: function (data) { SuccessHandler1(data); }
});
}
function SuccessHandler1(data) {
$('select#Slaveddl').empty();
$.each(data, function (i, slaveValue) {
$('select#Slaveddl').append(
$('<option></option>').val(slaveValue.Value).html(slaveValue.Text)
);
});
}
And My Handler:
public class SlaveValues {
public string Value { get; set; }
public string Text { get; set; }
}
public class MasterSlaveDropDownListsHandler : IHttpHandler {
public bool IsReusable {
get { return true; }
}
public void ProcessRequest(HttpContext context) {
string valueSelected = context.Request["ItemSelected"];
List<SlaveValues> slaveValues = new List<SlaveValues>();
SlaveValues sv;
sv = new SlaveValues();
sv.Text = "SV1";
sv.Value = valueSelected + "s1";
slaveValues.Add(sv);
sv = new SlaveValues();
sv.Text = "SV2";
sv.Value = valueSelected + "s2";
slaveValues.Add(sv);
string responseText =
Newtonsoft.Json.JsonConvert.SerializeObject(slaveValues);
context.Response.ContentType = "text/json";
context.Response.Write(responseText);
}
}
but there is nothing to append.
Also I see the response in firebug windows as following(when I Select G2 from Master ddl):
[{"Value":"G2s1","Text":"SV1"},{"Value":"G2s2","Text":"SV2"}]
and finally I change my success method of script with this new one for test:
function SuccessHandler2(data) {
$('select#Slaveddl').empty();
$.each(data, function (i, slaveValue) {
$('select#Slaveddl').append(
$('<option></option>').val('Member' + i).html('Member' + i)
);
});
}
when try this new script the binding to Slave ddl work correctly but with some additional items : the index show member0 to member30 and I don' know why.
Edit1:
I also try this one and append correctly:
function SuccessHandler3(data) {
var values = [{ "Value": "G2s1", "Text": "SV1" }, { "Value": "G2s2", "Text": "SV2"}];
$('select#Slaveddl').empty();
$.each(values, function (i, slaveValue) {
$('select#Slaveddl').append(
$('<option></option>').val('Member' + slaveValue.Value).html('Member' +
slaveValue.Text)
);
});
}
So I think there is a problem with manipulate of return value (data).
for more specific view the following pic is the JSON tab in firebug windows when I select G3 in Master ddl:
Edit2:
Also I try this one and just the first alert appear, apparently the (data.d) is null or unknown object:
function SuccessHandler6(data) {
var selection = $('select#Slaveddl');
$(selection).children().remove();
alert('in handler and remove children correctly');
if (data.d) {
alert('data.d is not null');
$(data.d).each(function (index, item) {
$(selection).append('<option>').val(item.Value).html(item.Text);
alert('after append in index: ' + index);
});
}
}
How can I use return value correctly and append to Slave ddl? Where is the problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
也许这非常有用,就像您的问题有一个完整的逐步解决方案一样:
在jquery中使用json的返回值
Maybe this can be useful this very like as your problem an have a complete step by step solution:
use return value of json in jquery
我想你应该这样尝试。
看看这个。
http://jsfiddle.net/rTua4/1/
为您的下一个操作尝试这个。
}
u should try like that i think.
check this out.
http://jsfiddle.net/rTua4/1/
for your next operation try this.
}
此链接将为您提供帮助。您只需要获取要在从属 ddl 中显示的数据部分。要仅获取该部分,您需要获取 data.d ..
在此处阅读更多内容:Eval response.d in Jquery Ajax
这是一个小示例也:
This link will help you. You need to get only the data part to show in your slave ddl. To get only that part you need to get data.d ..
Read More here : Eval response.d in Jquery Ajax
here is a small example too:
我想指出的是,您尝试使用 HTTP 处理程序完成的任务通常是使用 Web 服务或更准确地说是 WCF RESTful Web 服务来完成的。 WCF 甚至可以为您处理 JSON 的序列化。这是一个简化的示例,您可以根据自己的需要进行定制。 如何从 WCF 服务返回干净的 JSON?
据我所知,HTTP 处理程序用于处理文件类型(例如,您可以将处理程序与“.jpeg”文件类型关联以动态返回“.jpeg”图像)。
这是处理这个问题的“正确”方法。
I want to point out that what you're attempting to accomplish with the HTTP handler is typically accomplished using a Web Service, or more accurately a WCF RESTful Web Service. WCF even handles the serializing of JSON for you. Here's a simplified example that you can just tailor to your needs. How do I return clean JSON from a WCF Service?
As far as I know, HTTP handlers are used to handle file types (for instance, you can associate a handler with a ".jpeg" file type to dynamically return ".jpeg" images).
It's the "proper" way to handle this.