在asp.net mvc

时间:2016-01-15 07:34:52

标签: javascript asp.net-mvc

我想在用户取消选中该复选框时禁用下拉列表。它应该很简单,但我的代码不起作用......我的代码是: -

<div>
    @Html.CheckBox("Use_Favicon_Image", true, new { id= "1" })
    <label for="Use_Favicon_Image">Want to Use favicon</label>
</div>
<div class="row">
    <div class="input-field col s12">
      <p>Select Favicon Image </p>
      @Html.DropDownList("FaviconImage", new List<SelectListItem> { new SelectListItem { Text = "1.jpg" }, new SelectListItem { Text = "2.jpg" }, new SelectListItem { Text = "3.jpg" }, }, new { id = "2" })
    </div>
</div>

<script type="text/javascript">
    $(function () {
        $('#1').change(function () {
            if ($(this).is(':checked')) {
                // disable the dropdown:
                $('#2').attr('disabled', 'disabled');
            } else {
                $('#2').removeAttr('disabled');
            }
        });
    });
</script>

2 个答案:

答案 0 :(得分:1)

    $(function () {
        $('#1').change(function () {
            if ($(this).is(':checked')) {
                // disable the dropdown:
                $('#2').prop('disabled', true);
            } else {
                $('#2').prop('disabled', false);
            }
        });
    });

答案 1 :(得分:0)

试试这段代码:

$(function () {
    $('#1').change(function () {
        $('#2').prop('disabled', !$(this).is(':checked'))                
    });
});