我是 Visual Studio 2010 的新手。我有一个下拉列表,可以从基本的 select from 语句中提取数据列表。下拉列表中只有 4 个选项。假设只有苹果、香蕉、梨和葡萄。我如何做到这一点,当我选择苹果时它会重定向到一个页面,但是如果我选择香蕉它会重定向到另一个页面?目前我有提交按钮指向一个页面,但我如何让它根据用户选择动态变化?
问问题
3938 次
2 回答
2
处理 select 元素的 onchange 事件:
function getComboA(sel) {
var value = sel.options[sel.selectedIndex].value;
// decide what to do with value for example
if(value=='apple')
window.location='pages.aspx';
}
<asp:dropDownList onchange="getComboA(this);" ...
于 2013-01-04T17:55:11.727 回答
0
到这个时候你不需要渲染任何控制asp。
选项1
<select id="myselect" onchange="myfunction();">
<option value=""></option>
<option value="apple">apple</option>
<option value="banana">banana</option>
<option value="pear">pear</option>
<option value="grapes">grapes</option>
</select>
<script type="text/javascript">
function myfunction() {
var value = $("#myselect").val();
switch (value) {
case "apple":
window.location = "http://www.apple.com/";
break;
case "banana":
window.location = "http://www.google.com/";
break;
case "pear":
window.location = "http://www.apple.com/";
break;
case "grapes":
window.location = "http://www.apple.com/";
break;
}
}
</script>
选项 2
<asp:DropDownList ID="myselect" onchange="myfunction();" runat="server">
<asp:ListItem Text="apple" Value="apple" />
<asp:ListItem Text="banana" Value="banana" />
<asp:ListItem Text="pear" Value="pear" />
<asp:ListItem Text="grapes" Value="grapes" />
</asp:DropDownList>
<script type="text/javascript">
function myfunction() {
var value = $("#<%= myselect.ClientID %>").val();
switch (value) {
case "apple":
window.location = "http://www.apple.com/";
break;
case "banana":
window.location = "http://www.google.com/";
break;
case "pear":
window.location = "http://www.apple.com/";
break;
case "grapes":
window.location = "http://www.apple.com/";
break;
}
}
</script>
选项 3(需要回发)
<asp:DropDownList ID="myselect" runat="server" OnSelectedIndexChanged="myselect_SelectedIndexChanged" AutoPostBack="True">
<asp:ListItem Text="apple" Value="apple" />
<asp:ListItem Text="banana" Value="banana" />
<asp:ListItem Text="pear" Value="pear" />
<asp:ListItem Text="grapes" Value="grapes" />
</asp:DropDownList>
在 .cs 后面的代码中
protected void myselect_SelectedIndexChanged(object sender, EventArgs e)
{
switch (myselect.SelectedValue)
{
case "apple":
Response.Redirect("http://www.apple.com/");
break;
case "banana":
Response.Redirect("http://www.apple.com/");
break;
case "pear":
Response.Redirect("http://www.apple.com/");
break;
case "grapes":
Response.Redirect("http://www.apple.com/");
break;
}
}
于 2013-01-04T20:01:58.480 回答