[英]How to run DropDownList change event
您好,我尝试更改ddl.selectedIntex = 1,我看到ddl的值正在更改,但事件ddl.change不起作用。
$("#ctl00_PlaceHolderMain_AccountsDDL_ddlAccounts")[0].selectedIndex = 1;
这只是设置值,但我需要运行.change函数
有任何想法吗? 我在控制台中尝试了所有方法,我知道您如何通过将PostData传递给HttpWebRequest来做到这一点?
我在控制台中尝试了所有方法,我知道我如何通过将PostData传递给HttpWebRequest来做到这一点,我添加了我的代码
HttpWebRequest postRequest = (HttpWebRequest)WebRequest.Create("https://www.com/Pages/current.aspx");
PostString += "ctl00$PlaceHolderMain$AccountsDDL$ddlAccounts=" + Number + "&";//Here
byte[] byteArray = Encoding.ASCII.GetBytes(PostString);
postRequest.ContentLength = byteArray.Length;
Stream newStream = postRequest.GetRequestStream();
newStream.Write(byteArray, 0, byteArray.Length);
newStream.Close();
HttpWebResponse postResponse = (HttpWebResponse)postRequest.GetResponse();
通过代码更改值时,不会触发更改事件。 您需要调用change()
或trigger("change")
$("#ctl00_PlaceHolderMain_AccountsDDL_ddlAccounts").change()
要么
$("#ctl00_PlaceHolderMain_AccountsDDL_ddlAccounts").trigger("change")
作为附加说明,请使用Control.ClientID来获取javascript中的元素,而不要使用经过硬编码的asp.net生成的客户端ID。
$("#<%= ddlAccounts.ClientID %>").change()
或只是使用这个
$("[id$=ddlAccounts]").change();
使用$'Ends with'来缩短选择器的名称
<script>
$("select").change(function () {
var str = "";
$("select option:selected").each(function () {
str += $(this).text() + " ";
$("#DropDownList1")[0].selectedIndex = 5;
});
$("div").text(str);
}).change();
</script>
<div></div>
试试这个...。它可能对您有帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.