我尝试使用MVC,我不知道如何通过将dropdownlist的值传递给事件onchange。
@Html.DropDownList("cblist_id", (IEnumerable<SelectListItem>)ViewBag.listproduct, new { size = 5, @class = "form-control", onchange = "GetProduct(" + this.value + ", 123)" })
这是脚本中的功能
function GetProduct(productid, categoryid) {
////do something
}
我为示例
收到此错误Compiler Error Message: CS1061: 'ASP._Page_Areas_Users_Views_Permit_GrandPermit_cshtml' does not contain a definition for 'value' and no extension method 'value' accepting a first argument of type 'ASP._Page_Areas_Users_Views_Permit_GrandPermit_cshtml' could be found (are you missing a using directive or an assembly reference?)
所以,我认为当我尝试获取下拉列表的值时问题。
答案 0 :(得分:1)
这里的this.value
是指服务器端C#的上下文,您需要绑定JavaScript this
下拉列表的onchange = "GetProduct(this, 123)"
@Html.DropDownList("cblist_id", (IEnumerable<SelectListItem>)ViewBag.listproduct, new { size = 5, @class = "form-control", onchange = "GetProduct(this, 123)" })
使用您的功能,GetProduct
执行以下操作
function GetProduct(drp, number){
$(drp).val()
// or
drp.value
}
和其他东西一样直接传递值
@Html.DropDownList("cblist_id", (IEnumerable<SelectListItem>)ViewBag.listproduct, new { size = 5, @class = "form-control", onchange = "GetProduct(this.value, 123)" })
function GetProduct(selectedValue, number){
}