我正试图从DropDownListFor中获取所选项的值,并将其作为参数传递。
我的代码看起来像:
@Html.DropDownListFor(x => x.SelectedFileName,
Model.Files,
new
{
Name = "map",
@class = "form-control",
@onchange = "CallChangefunc('TheSelectedItemAsString');"
})
我的意思是,我被这个弄糊涂了:@onchange = "CallChangefunc('TheSelectedItemAsString');"
我需要类似的东西:@onchange = "CallChangefunc('"+x.Value+"');"
CallChangefunc
看起来像:
<script>
function CallChangefunc(x) {
//some code
}
</script>
在Jquery
:中这样尝试
$("#map").change(function () {
var selectedValue = this.value;
alert("Selected Value: " + selectedValue);
});
或在javascript 中
@onchange = "CallChangefunc(this);
并在CallChangefunc
函数中获取下拉列表的值
function CallChangefunc(ddl) {
var selectedValue = ddl.value;
alert("Selected Value: " + selectedValue);
}
用以下代码替换下拉列表
@Html.DropDownListFor(x => x.SelectedFileName,
Model.Files,
new
{
@class = "form-control somevalue"
})
然后像一样为此写入CCD_ 6
<script>
var selectedDropDownValue;
$(document).on('change', "select.somevalue", function () {
selectedDropDownValue = $(this).val();
});
</script>