Html 如何修改';当用户更改a<;中的选定选项时,浏览器中当前显示的<;选择>;元素?(asp.net)
对不起,我对web开发和ASP.Net不熟悉。当用户更改Html 如何修改';当用户更改a<;中的选定选项时,浏览器中当前显示的<;选择>;元素?(asp.net),html,asp.net,asp.net-mvc,razor,Html,Asp.net,Asp.net Mvc,Razor,对不起,我对web开发和ASP.Net不熟悉。当用户更改元素中的选定选项时,如何动态修改浏览器中当前显示的HTML @*DropDown:*@ <div class="form-group"> @Html.LabelFor(model => model.CategoryId, "CategoryId", htmlAttributes: new { @class = "control-label col-md-2" }) <div c
元素中的选定选项时,如何动态修改浏览器中当前显示的HTML
@*DropDown:*@
<div class="form-group">
@Html.LabelFor(model => model.CategoryId, "CategoryId", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("CategoryId", null, htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.CategoryId, "", new { @class = "text-danger" })
</div>
</div>
@*changes:*@
@if (Model.CategoryId == 3)
{
string s = "stuff";
<h1>s</h1>
}
@*下拉列表:*@
@LabelFor(model=>model.CategoryId,“CategoryId”,htmlAttributes:new{@class=“control label col-md-2”})
@DropDownList(“CategoryId”,null,htmlAttributes:new{@class=“form control”})
@Html.ValidationMessageFor(model=>model.CategoryId,“,new{@class=“text danger”})
@*变化:*@
@如果(Model.CategoryId==3)
{
字符串s=“stuff”;
s
}
通常,您可以使用JavaScript将更改事件附加到dropdownlist来完成此操作
以下是jQuery(一种流行的JavaScript框架)的情况:
@LabelFor(model=>model.CategoryId,“CategoryId”,htmlAttributes:new{@class=“control label col-md-2”})
@DropDownList(“CategoryId”,null,htmlAttributes:new{@class=“form control”})
@Html.ValidationMessageFor(model=>model.CategoryId,“,new{@class=“text danger”})
$(函数(){
$('#CategoryId')。关于('change',函数(e){
var categoryId=此值;
//根据所选值对HTML进行填充
if(parseInt(categoryId)==3){
$('h1').text('stuff');
}
});
});
通常,您可以使用JavaScript将更改事件附加到dropdownlist来完成此操作
以下是jQuery(一种流行的JavaScript框架)的情况:
@LabelFor(model=>model.CategoryId,“CategoryId”,htmlAttributes:new{@class=“control label col-md-2”})
@DropDownList(“CategoryId”,null,htmlAttributes:new{@class=“form control”})
@Html.ValidationMessageFor(model=>model.CategoryId,“,new{@class=“text danger”})
$(函数(){
$('#CategoryId')。关于('change',函数(e){
var categoryId=此值;
//根据所选值对HTML进行填充
if(parseInt(categoryId)==3){
$('h1').text('stuff');
}
});
});
您是指下拉菜单吗?您是指下拉菜单吗?
<div class="form-group">
@Html.LabelFor(model => model.CategoryId, "CategoryId", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("CategoryId", null, htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.CategoryId, "", new { @class = "text-danger" })
</div>
</div>
<h1></h1>
<script>
$(function() {
$('#CategoryId').on('change', function (e) {
var categoryId = this.value;
//do stuff to HTML based on selected value
if (parseInt(categoryId) === 3) {
$('h1').text('stuff');
}
});
});
</script>