Javascript 在MVC中,如何将dropdownlist的值传递给事件onchange?
我尝试使用MVC,但我不知道如何将dropdownlist的值传递给EventOnChangeJavascript 在MVC中,如何将dropdownlist的值传递给事件onchange?,javascript,jquery,model-view-controller,onchange,html.dropdownlistfor,Javascript,Jquery,Model View Controller,Onchange,Html.dropdownlistfor,我尝试使用MVC,但我不知道如何将dropdownlist的值传递给EventOnChange @Html.DropDownList("cblist_id", (IEnumerable<SelectListItem>)ViewBag.listproduct, new { size = 5, @class = "form-control", onchange = "GetProduct(" + this.value + ", 123)" }) 我得到这个错误的样本 Compiler
@Html.DropDownList("cblist_id", (IEnumerable<SelectListItem>)ViewBag.listproduct, new { size = 5, @class = "form-control", onchange = "GetProduct(" + this.value + ", 123)" })
我得到这个错误的样本
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?)
因此,我认为当我尝试获取dropdownlist的值时会出现问题。这里的
this.value
指的是服务器端C#的上下文,您需要在JavaScriptonchange=“GetProduct(this,123)”上绑定下拉列表的this
和其他直接传递值的方法,就像这样
@Html.DropDownList("cblist_id", (IEnumerable<SelectListItem>)ViewBag.listproduct, new { size = 5, @class = "form-control", onchange = "GetProduct(this.value, 123)" })
function GetProduct(selectedValue, number){
}
@Html.DropDownList(“cblist_id”,(IEnumerable)ViewBag.listproduct,新的{size=5,@class=“form control”,onchange=“GetProduct(this.value,123)”)
函数GetProduct(selectedValue、number){
}
我知道了。我使用$(drp).val()
。非常感谢。
@Html.DropDownList("cblist_id", (IEnumerable<SelectListItem>)ViewBag.listproduct, new { size = 5, @class = "form-control", onchange = "GetProduct(this, 123)" })
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){
}