选择下拉列表中的值时隐藏表单
Hide form when value from dropdown is chosen
我有这些表格。
<div class="form-group">
@Html.LabelFor(model => model.RoomsNumber, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.RoomsNumber, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.RoomsNumber, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.ProductCategoryId, "Type of home", htmlAttributes: new { @class = "control-label col-md-2"})
<div class="col-md-10">
@Html.DropDownList("ProductCategoryId", null, htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.ProductCategoryId, "", new { @class = "text-danger" })
</div>
</div>
我希望当我从下拉列表中选择某个值以隐藏带有房间号的表单时。我不知道该怎么做。
您可以使用 JavaScript 轻松完成此操作,您需要为房间表单提供一个 ID,为产品类别下拉列表需要一个 ID,然后您需要添加一个事件侦听器来收听下拉列表的更改事件:
.JS
var ddl = document.getElementById('ddlProductCategory'),
form = document.getElementById('roomsForm');
ddl.addEventListener('change', function(){
if (this.value === '5'){
form.style.display = 'none';
}
else {
form.style.display = 'block';
}
});
.HTML
<div class="form-group" id="roomsForm">
@Html.LabelFor(model => model.RoomsNumber, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.RoomsNumber, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.RoomsNumber, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.ProductCategoryId, "Type of home", htmlAttributes: new { @class = "control-label col-md-2"})
<div class="col-md-10">
@Html.DropDownList("ProductCategoryId", null, htmlAttributes: new { @class = "form-control", id = 'ddlProductCategory" })
@Html.ValidationMessageFor(model => model.ProductCategoryId, "", new { @class = "text-danger" })
</div>
</div>
相关文章:
- 使用javascript取消隐藏和隐藏表单中的某些元素
- 如何使用javascript隐藏表单的选项
- 模拟隐藏表单提交&使用JavaScript重定向以实现安全支付
- 未执行Ajax隐藏表单提交验证
- 在同一个html页面中显示servlet响应而不隐藏表单
- 自动提交隐藏表单值
- 如何筛选出发布的隐藏表单输入类型
- 如何在angularjs中提交后隐藏表单
- 如果用户登录了Backbone,JavaScript,如何显示/隐藏表单
- 显示/隐藏表单区域(im 使用引导程序)
- 如何隐藏表单并仅在选中时显示
- 如何通过隐藏表单在同一页面中显示表单输出
- 如何在同一窗口中显示表单输出并隐藏表单
- 使用 jquery 选择选项时隐藏表单选项
- 当一组元素的所有元素都具有特定值时,如何隐藏表单元素
- 如果禁用了javascript,则隐藏表单
- 选择下拉列表中的值时隐藏表单
- 无法隐藏/取消隐藏表单内的按钮
- 如何根据最后选择的选项(单选按钮)使用 javascript 隐藏表单输入
- 显示和隐藏表单