2013-05-01 47 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