ASP。NET MVC4 +下拉列表+ onChange js事件

时间:2022-11-15 04:02:08

So I've got a dropdownlist in my View History:

所以我在历史课上有一个下拉列表:

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

and I have this js event to replace the url ../History/ to ../History/2, ../History/5

我有这个js事件来替换url。/历史/ . ./历史/ 2 . . /历史/ 5

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

Instead it always adds the value to the current url. So I get History/2/5 for example after changing index in dropdown.

相反,它总是将值添加到当前url。所以我得到历史/2/5,例如在下拉菜单中改变索引。

1 个解决方案

#1


2  

You are not getting selected value correctly:

您没有正确地获得所选值:

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

And here is structure of the current page url:

这是当前页面url的结构:

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

#1


2  

You are not getting selected value correctly:

您没有正确地获得所选值:

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

And here is structure of the current page url:

这是当前页面url的结构:

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