【发布时间】:2020-07-26 08:03:25
【问题描述】:
我有选择选项,例如,
<td>
<select id="ddlregion" name="RegionId">
<option value="0">Select Region</option>
@foreach (Region re in region)
{
<option value="@re.RegionID">@re.RegionName</option>
}
</select>
</td>
<td>
<select disabled id="ddlcity" name="CityId">
<option value="0">Select City</option>
@foreach (City re in city)
{
<option data-region="@re.RegionID" value="@re.CityID">@re.CityName</option>
}
</select>
</td>
Onchange 我正在调用方法,
$("body").on("change", "#ddlregion", function () {
var selectedRegion = $(this).val();
$('#ddlcity').prop('disabled', false);
$("#ddlcity").find("option").each(function () {
if ($(this).data("region") == selectedRegion) {
$(this).show();
} else {
$(this).hide();
}
});
});
一切正常。
现在我尝试在 HTML 助手 DropDownList 中使用相同的方法,
<div class="form-group">
@Html.LabelFor(model => model.RegionId, "RegionId", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("RegionId", null, new { @class = "form-control"})
@Html.ValidationMessageFor(model => model.RegionId, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.CityId, "CityId", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("CityId", null, htmlAttributes: new { @class = "form-control", @data_region="RegionId" })
@Html.ValidationMessageFor(model => model.CityId, "", new { @class = "text-danger" })
</div>
</div>
$("body").on("change", "#RegionId", function () {
var selectedRegion = $(this).val();
alert("test");
$('#CityId').prop('disabled', false);
$("#CityId").find("option").each(function () {
if ($(this).data("region") == selectedRegion) {
$(this).show();
} else {
$(this).hide();
}
});
});
using above script it not not calling on change.
比我在区域下拉列表中进行更改并像这样更改脚本,
@Html.DropDownList("RegionId", null, new { @class = "form-control",onchange="callChangefunc(this.value)"})
function callChangefunc(val) {
var selectedRegion = $(this).val();
alert("test");
$('#CityId').prop('disabled', false);
$("#CityId").find("option").each(function () {
if ($(this).data("region") == selectedRegion) {
$(this).show();
} else {
$(this).hide();
}
});
}
我收到了这个错误,
未捕获的类型错误:无法读取未定义的属性“toLowerCase”
希望您的建议
【问题讨论】:
标签: c# jquery asp.net-mvc model-view-controller html-helper