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

JavaScript查找所选下拉项的索引,传递到超链接,javascript,asp.net,dom,sharepoint-2010,Javascript,Asp.net,Dom,Sharepoint 2010,我遇到了一些麻烦,经过相当多的研究,一直无法找到解决办法。我在SharePoint Designer 2010中工作,有一个由列表填充的ASP.net下拉列表。我想从下拉列表中获取所选项目的索引值(例如1),并将其传递到用于打开EditForm.aspx页面的URL。请参阅下文,并感谢您提供的任何帮助 <script type="text/javascript"> function redirect(url) { var ddl = document.getE

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

<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" />

函数重定向(url){
var ddl=document.getElementById(&apos;DropDownList1&apos;);
警惕(“嗨!”);
var指数=ddl.selectedIndex;
var value=ddl.options[index].value;
location.href=url+值;
返回false;
}
编辑系统信息

您应该使用呈现的ID:

var ddl = document.getElementById('<%=DropDownList1.ClientID%>');
要获取索引,只需使用

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" />

函数重定向(url){
var ddl=document.getElementById(“”),
索引=ddl.selectedIndex,
value=ddl.options[index].value;
location.href=url+值;
返回false;
}

请为
DropDownList1
添加代码。我认为我的JavaScript函数不会因为某种原因被调用。我添加了“代码”警报(url+值);'“代码”并且没有出现任何问题。您必须使用
OnClientClick
事件。更新了我的答案。非常感谢您对代码的帮助。我移动了脚本,使它正好位于下面,看起来这是正确的移动,但现在我收到一个错误,说“此文件中不允许使用代码块”?是否有javascript应该放置在的特定区域?
<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" />