我有一个下拉列表,当我从列表中选择一个项目时,它会打开一个新窗口。如何将下拉列表值传递到 URL 中的新窗口?
我的代码是这样的:
<asp:DropDownList ID="ddlAdd" runat="server" CssClass="ddl" OnChange="javascript:openWindow('add.aspx?ddlAddValue=', 800, 885)">
<asp:ListItem>One</asp:ListItem>
<asp:ListItem>Two</asp:ListItem>
<asp:ListItem>Three</asp:ListItem>
</asp:DropDownList>
这是 JavaScript 函数:
function openWindow(url, windowHeight, windowWidth)
{
var centerHeight = (screen.height - windowHeight) / 2;
var centerWidth = (screen.width - windowWidth) / 2;
var features = "height=" + windowHeight + ", width=" + windowWidth + ", top=" + centerHeight + ", left=" + centerWidth + ", scrollbars=" + 1;
var popUp = window.open(url, "", features);
}
最佳答案
您可以简单地引用this.value
来获取底层select 的值。
OnChange="javascript:openWindow('add.aspx?ddlAddValue=' + escape(this.value), 800, 885)"
此外,这在 jQuery 中很容易处理,如果您是这样的话:
$(function(){
$('select[id$=ddlAdd]').change(function () {
var addValue = $(this).val();
if (addValue) {
// window opening logic here
}
return false;
});
});
关于javascript - 在 URL 中传递下拉列表的选定值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11800746/