我有一个下拉列表,我在其中调用js函数,在change事件上。 在查看:将ViewBag传递给js函数
@Html.DropDownList("RaceId", ViewData["Races"] as List<SelectListItem>, new { @onchange = "CallChangefunc(this.value)", @class="form-control" })
和我的JS:
<script>
function CallChangefunc(val)
{
//called in Index page when dropdown list changes
window.location.href = "/Index/" + val;
}
</script>
我要的是一个新的参数添加到我的js函数,我传递一个ViewBag值,某事像:
@Html.DropDownList("RaceId", ViewData["Races"] as List<SelectListItem>, new { @onchange = "CallChangefunc(this.value,ViewBag.id)", @class="form-control" })
上述不起作用,我不确定哪个是正确的语法,如果有的话。
使用[非侵入式JavaScript](https://en.wikipedia.org/wiki/Unobtrusive_JavaScript)(其在21世纪) - '$(#RaceId')。改变(函数(){var val = $(this).val(); var id ='@ ViewBag.id'; ......});' –
@ stephenmuecke的评论需要jQuery,并且应该读$('#RaceId').....但他说得很好! – wazdev