JavaScript查找选定下拉项目的索引,传递到超链接

JavaScript to Find Index of Selected Dropdown Item, Pass to Hyperlink

本文关键字:索引 超链接 项目 查找 JavaScript      更新时间:2023-09-26

我遇到了一些麻烦,经过相当多的研究,一直无法找到解决方案。我在SharePoint Designer 2010工作,有一个ASP.net下拉列表填充。我想从下拉列表中获得所选项目的索引值(例如1),并将其传递给用于调出EditForm的URL。aspx页面。见下文,感谢您提供的任何帮助!

<script type="text/javascript">
    function redirect(url) {
        var ddl = document.getElementById(&apos;DropDownList1&apos;);
        alert(&quot;HI!&quot;);
        var index = ddl.selectedIndex;
        var value = ddl.options[index].value;
        location.href = url + value;
        return false;
    }
</script>
<asp:LinkButton runat="server" id="LinkButton1"
                href="https://chartiscorp.sp.ex3.secureserver.net/Lists/System_Information/EditForm.aspx?id="
                onclientclick="javascript:redirect(this.href)">Edit System Info</asp:LinkButton>
<asp:DropDownList runat="server" id="DropDownList1" DataValueField="Title"
                  DataTextField="Title" DataSourceID="spdatasource1" />

您应该使用呈现的ID:

var ddl = document.getElementById('<%=DropDownList1.ClientID%>');

LinkButtonOnClientClick事件:

<asp:LinkButton onclientclick="...">

使用

获取索引
var index = ddl.selectedIndex;

或者如果你想获得值,使用

var value = ddl.options[ddl.selectedIndex].value;
我建议在函数中进行重定向,而不是在HTML属性中。集合:
<script type="text/javascript">
    function redirect(url) {
        var ddl = document.getElementById('<%=DropDownList1.ClientID%>'),
            index = ddl.selectedIndex,
            value = ddl.options[index].value;
        location.href = url + value;
        return false;
    }
</script>
<asp:LinkButton runat="server" id="LinkButton1"
                href="../Lists/System_Information/EditForm.aspx?id="
                onclientclick="redirect(this.href)">LinkText</asp:LinkButton>
<asp:DropDownList runat="server" id="DropDownList1" DataValueField="Title"
                  DataTextField="Title" DataSourceID="spdatasource1" />