【发布时间】:2015-03-31 14:51:19
【问题描述】:
我正在尝试使用 JQuery AJAX 链接两个下拉列表,但我什么也没得到。
我的下拉菜单的 ASP.NET 代码是:
<td><a href="#" title="Choose the park that you would like. Mandatory Field.">Park*</a></td><!-- PARK -->
<td>
<asp:DropDownList class="form-control" ID="parkDDL" ClientIDMode="Static" onchange="ShowCurrentBuilding()" style="width:150px;" runat="server">
<asp:ListItem text="ALL" value="ALL"></asp:ListItem>
<asp:ListItem text="Central" value="C"></asp:ListItem>
<asp:ListItem text="West" value="W"></asp:ListItem>
<asp:ListItem text="East" value="E"></asp:ListItem>
</asp:DropDownList>
</td>
<td><a href="#" title="Choose the building that you would like">Building</a> </td><!-- BUILDING -->
<td>
<asp:DropDownList class="form-control" AutoPostBack="true" ID="buildingDDL" runat="server" style="width:300px;" OnSelectedIndexChanged="buildingToRoom"></asp:DropDownList>
</td>
我在 JS 标记之间的 JQuery 代码(如您所见,我不太确定如何将返回的内容输出到下拉列表):
function ShowCurrentBuilding() {
$.ajax(
{
type: "POST",
url: "CreateRequest.aspx/GetCurrentBuilding",
data: '{name: ' + $('#<%= parkDDL.ClientID %> option:selected').val() + '}',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: OnSuccess,
failure: function (response) {
alert(response.d);
}
});
}
function OnSuccess(response) {
alert(response.d);
}
我还尝试了以下我在文档准备功能中放入的内容:
$(document).ready(function () {
$('#<%=parkDDL.ClientID %>').on("change",function ShowCurrentBuilding() {
$.ajax(
{
type: "POST",
url: "CreateRequest.aspx/GetCurrentBuilding",
data: '{name: "' + $(this).find("option:selected").val() + '"}',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: OnSuccessBuilding,
failure: function (response) {
alert(response.d);
}
});
});
function OnSuccessBuilding(response) {
alert(response.d);
}
});
最后,这是我的 C# 代码:
namespace Team11
{
public partial class CreateRequest : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//populate my initial drop down lists etc
}
}
//here is the function that is called in the JQuery function
[System.Web.Services.WebMethod]
public string GetCurrentBuilding(string name)
{
string textbx = "";
textbx = name;
return textbx;
}
我只想通过 ajax 过滤下拉列表,这样页面就不会重新加载 AutoPostBack = "true"。
提前感谢任何可以提供帮助的人!
【问题讨论】:
-
我很确定 OnSelectedChanged 是一个特定的服务器端 .net 事件。如果要执行客户端功能,请尝试 onchange。
-
放置 onchange 而不是 OnSelectedChanged。您是否在此方法中放置了任何断点: public static ArrayList GetCurrentBuilding(string name) ?
-
不,我会放什么样的断点?我指定的功能在我的问题的底部。我没有添加任何其他内容。谢谢。
-
他在询问服务器端方法中的断点,以查看是否已到达。无论如何,您是否尝试过 onchange?
-
是的,我试过了,它消除了错误,但我怎样才能将它输出到下拉列表中。我正在返回一个数组列表,但我不确定如何在下拉列表中显示它。另外,我什至不确定如何测试我是否得到了列表,因为我不知道如何输出 ArrayList。
标签: javascript c# jquery asp.net ajax