2012-11-28 39 views
3

所以我有一個下拉列表在我的瀏覽歷史:ASP.NET MVC4 +下拉列表+ JS的onChange事件

@Html.DropDownList("ddlAccounts", null, new { @onchange="onChange(this.value);" }) 

,我有這個js事件替換URL ../History/到../History/2,../History/5

function onChange(value) { 
    window.location.href = value; 
} 

相反,它總是將值添加到當前url。因此,例如在更改下拉菜單中的索引後,我得到History/2/5。

+0

它不需要用「@」在htmlAttributes字典onchange屬性添加,僅在c#中使用前綴作爲關鍵字的屬性,例如@class –

回答

2

你沒有得到選定值正常:

@onchange="onChange(this.options[this.selectedIndex].value);" 

這裏是當前頁面的URL的結構:

hash: "#13612872" 
host: "stackoverflow.com" 
hostname: "stackoverflow.com" 
href: "http://stackoverflow.com/questions/13612838/asp-net-mvc4-dropdownlist-onchange-js-event/13612872#13612872" 
origin: "http://stackoverflow.com" 
pathname: "https://stackoverflow.com/questions/13612838/asp-net-mvc4-dropdownlist-onchange-js-event/13612872" 
port: "" 
protocol: "http:" 
+0

謝謝!稍微調整了我的代碼。 – 8bitboy

+0

歡迎您:) – karaxuna