gpt4 book ai didi

jquery - 使用 JavaScript 以编程方式触发 ASP.net CascadingDropDown 更改事件的正确方法

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

标记

<asp:ScriptManager runat="server" />
Country Code
<asp:TextBox ID="CoutryCodeTextBox" runat="server" onblur="selectCountry(this.id);">
</asp:TextBox>

<asp:DropDownList ID="CountryDropDownList" runat="server">
</asp:DropDownList>

<ajaxToolkit:CascadingDropDown
ID="CountryDropDownListCascadingDropDown" runat="server"
TargetControlID="CountryDropDownList"
Category="Country"
ServiceMethod="GetCountries"
ServicePath="~/CountryData.asmx"
LoadingText="Loading ..."
PromptText="SELECT">
</ajaxToolkit:CascadingDropDown>


<asp:DropDownList ID="CityDropDownList" runat="server">
</asp:DropDownList>
<ajaxToolkit:CascadingDropDown
ID="CityDropDownListCascadingDropDown" runat="server"
ParentControlID="CountryDropDownList"
TargetControlID="CityDropDownList"
Category="City" ServiceMethod="GetCities"
ServicePath="~/CountryData.asmx"
LoadingText="Loading ..."
PromptText="SELECT">
</ajaxToolkit:CascadingDropDown>

Web 服务 (~/CountryData.asmx)

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
[ScriptService]
public class CountryData : System.Web.Services.WebService
{
[WebMethod]
public CascadingDropDownNameValue[] GetCountries(string knownCategoryValues, string category)
{
List<CascadingDropDownNameValue> values = new List<CascadingDropDownNameValue>();

values.Add(new CascadingDropDownNameValue("United States", "US"));
values.Add(new CascadingDropDownNameValue("Canada", "CA"));

return values.ToArray();
}

[WebMethod]
public CascadingDropDownNameValue[] GetCities(string knownCategoryValues, string category)
{
StringDictionary kv = CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues);
string country = kv["Country"];

List<CascadingDropDownNameValue> values = new List<CascadingDropDownNameValue>();

switch (country)
{
case "US":
values.Add(new CascadingDropDownNameValue("California", "CA"));
values.Add(new CascadingDropDownNameValue("New York", "NY"));
break;
case "CA":
values.Add(new CascadingDropDownNameValue("Toronto", "TO"));
values.Add(new CascadingDropDownNameValue("Montreal", "MO"));
break;
}

return values.ToArray();
}

}

jQuery

    var selectCountry = function (id)
{
var countryCodeTextBox = $("#" + id);
var countryDropDownList = $("#CountryDropDownList");

countryDropDownList.val(countryCodeTextBox.val());
countryDropDownList.change();
}

JavaScript 函数更改 CountryDropDownList 的选定值。但是,级联控件 CityDropDownList 不会自动填充。

使用 jQuery 触发父控件中的更改事件以便相关控件自动级联的正确方法是什么?

最佳答案

根据您的说法,my answer on How can I trigger an onchange event manually?也解决了您的问题:

There's a couple of ways you can do this. If the onchange listener is a function set via the element.onchange property and you're not bothered about the event object or bubbling/propagation, the easiest method is to just call that function:

element.onchange();

If you need it to simulate the real event in full, or if you set the event via the html attribute or addEventListener/attachEvent, you need to do a bit of feature detection to correctly fire the event:

if ("createEvent" in document) {
var evt = document.createEvent("HTMLEvents");
evt.initEvent("change", false, true);
element.dispatchEvent(evt);
}
else
element.fireEvent("onchange");

关于jquery - 使用 JavaScript 以编程方式触发 ASP.net CascadingDropDown 更改事件的正确方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17754972/

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