2013-05-01 29 views
0

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

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

请添加代码'DropDownList1'。 – 2013-05-01 11:30:02

+0

我在 – user2339121 2013-05-01 11:38:30

回答

0

您应该使用呈现ID:

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

LinkButtonOnClientClick event

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

This jsfiddle shows a demo of the rendered output.

+0

以上添加了它没有看到您的更新回复谢谢! – user2339121 2013-05-01 11:27:33

+0

我不认为我的JavaScript函数因某种原因被调用。我添加了'code'alert(url + value);'code'并没有出现 – user2339121 2013-05-01 11:51:14

+0

您必须使用'OnClientClick'事件。更新了我的答案。 – 2013-05-01 11:54:24