【发布时间】:2018-02-21 08:43:34
【问题描述】:
我正在尝试添加代码,以便当用户选中某个框时,启用某个字段而禁用另一个字段,如果未选中该复选框,反之亦然。我在这里查看并找到了这段代码:
<head>
<script type="text/javascript">
$('#UseForecast').click(function() {
var $this = $(this);
if ($this.is(':checked')) {
$('#MinimumOnHandQuantity').removeAttr("disabled");
$('#ForecastMultiplier').attr("disabled", "disabled")
} else {
$('#MinimumOnHandQuantity').attr("disabled", "disabled")
$('#ForecastMultiplier').removeAttr("disabled");
}
});
</script>
</head>
@Html.EditorFor(model => model.UseForecast, new { htmlAttributes = new { @class = "form-control" } })
@Html.EditorFor(model => model.MinimumOnHandQuantity, new { htmlAttributes = new { @class = "form-control" } })
@Html.EditorFor(model => model.ForecastMultiplier, new { htmlAttributes = new { @class = "form-control" } })
我错过了什么?
我对其进行了修改以在我的页面上包含字段的 ID。 UseForecast 是复选框的 id,MinimumOnHandQuantity 是第一个文本框,ForecastMultiplier 是第二个文本框的 id。当我尝试单步执行代码时,它会在页面最初加载时停止,但只要我单击复选框,它就不会运行。
【问题讨论】:
-
您需要一个文档加载处理程序或将脚本移动到 html 元素下方。还要仔细检查 id 属性以确保帮助程序按照您的预期呈现 id。
-
你确定 UseForecast 是
CheckBox? -
@Jasen,下面在哪里?在页面底部?
-
@Jasen 我把它放在页面底部,它仍然什么都不做,但现在页面最初加载时它崩溃了
-
@AT-2017,我只是再次检查了一个检查元素,它说 type="checkbox"
标签: javascript asp.net-mvc razor