繁体   English   中英

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

[英]JQuery - Adding change event to a Html.DropDownList

抱歉,我是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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM