javascript onchange 事件到 asp.net 下拉列表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8750387/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
javascript onchange event to asp.net dropdownlist
提问by Anju Thapa
Is it possible to make the dropdown list selection trigger posting back to the same page with whatever was selected added to the url as querystring using javascript? The problem is the list is being loaded dynamically from some sharepoint list. if my site is mysite.com/default.aspx so when a selection is made, it should redirect to mysite.com/default.aspx?key=selection
是否可以使用 javascript 将选择的任何内容作为查询字符串添加到 url 中,使下拉列表选择触发器回发到同一页面?问题是列表是从某个共享点列表动态加载的。如果我的站点是 mysite.com/default.aspx,那么在进行选择时,它应该重定向到 mysite.com/default.aspx?key=selection
No server access, No access to codebehind :(
无法访问服务器,无法访问代码隐藏 :(
<asp:DropDownList runat="server" id="DropDownList1" DataSourceID="spdatasource1" DataValueField="CategoryName" AutoPostBack="True" Onchange="window.open( Go to some link)">
</asp:DropDownList>
回答by Christopher Marshall
var selectedOption = $("#DropDownList1 option:selected").text();
$("#DropDownList1").change(function(e) {
url: "mysite.com/default.aspx?key=" + selectedOption;
window.location = url;
});
Untested, also unsure of what event is reloading the page e.g. (submit or anchor)
未经测试,也不确定是什么事件正在重新加载页面,例如(提交或锚点)
Another option (possibly better) from http://tinyurl.com/82ter35
来自http://tinyurl.com/82ter35 的另一种选择(可能更好)
$(document).ready(function() {
var selectedOption = $("#DropDownList1 option:selected").text();
$("#DropDownList1").change(function() {
$.ajax({
type: "POST",
url: "mysite.com/default.aspx?key=" + selectedOption,
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(msg) {
// Replace the div's content with the page method's return.
alert("this worked!");
}
});
});
});
回答by Thair
I am not sure what you want to do , but I guess jquery is the right answer to your question
我不确定你想做什么,但我想 jquery 是你问题的正确答案
anyway this may help :
无论如何,这可能会有所帮助:
<script language="javascript" type="text/javascript">
function xx(e) {
alert("fired by " + "<%= DropDownList1.UniqueID %>" + "change ");
__doPostBack("<%= DropDownList1.UniqueID %>", "");
}
</script>
<asp:DropDownList ID="DropDownList1" runat="server" onchange="xx()">
<asp:ListItem>q</asp:ListItem>
<asp:ListItem>w</asp:ListItem>
<asp:ListItem>e</asp:ListItem>
<asp:ListItem></asp:ListItem>
</asp:DropDownList>
Code behind(VB.NET)
代码隐藏(VB.NET)
Protected Sub DropDownList1_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles DropDownList1.SelectedIndexChanged
MsgBox("Do whatever you want here")
End Sub