javascript - jQuery-将更改事件添加到Html.DropDownList

标签 javascript jquery html asp.net razor

抱歉,我是Web开发和ASP.Net的新手。当用户更改元素中的选定选项时,如何动态修改浏览器中当前显示的HTML?

   <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>

最佳答案

我找到了解决方案。您需要导入jquery。

<div class="form-group">
    @Html.LabelFor(model => model.CategoryId, "Category", 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 type="text/javascript" src="/scripts/jquery-3.1.1.js"></script>

<script>
    $(function() {
        $('#CategoryId').on('change', function (e) {
            var categoryId = this.value;

            //do stuff to HTML based on selected value
            if (parseInt(categoryId) === 1) {
                $('h1').text('stuff');
            }
        });
    });
</script>

关于javascript - jQuery-将更改事件添加到Html.DropDownList,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44116618/

相关文章:

javascript - jQuery - 一旦增加浏览器缩放级别,UI 就会中断

javascript - 如果ajax成功则关闭对话框

jquery - 在jquery dataTable中隐藏空列的最佳方法

html - 将小图像定位在文本左侧?

html - 当需要溢出属性时,Twitter Bootstrap 按钮下拉溢出问题

javascript - Flot条形图设计

javascript - 如何创建一个发送消息的函数

javascript - 访问路由器参数 VueJS

javascript - express - 如何在对 API 的请求中读取 HttpOnly cookie?

javascript - jQuery 在不使用 href 的情况下平滑滚动