复选框以启用或禁用列表框 [英] Checkbox to enable or disable a list box

查看:69
本文介绍了复选框以启用或禁用列表框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在创建的ASP.NET MVC页面上有一个复选框,如下所示:

I have a checkbox on my ASP.NET MVC page that I create like:

<div>
    @Html.CheckBoxFor(m => m.allUsers, new { Name = "allUsersCheck" })
    @Html.LabelFor(m => m.allUsers)
</div>

我还有一个创建时使用的列表框:

I also have a listbox that I create with:

@Html.ListBoxFor(x => x.selectedUsers, Model.ListofUsers, new { style = "width:200px", size = 10, Id = "lbUsers" })

我有以下脚本(我在选中复选框时禁用了列表框.我不明白为什么它不起作用.有人可以指出我的代码有什么问题吗?

I have this following script (which I wrote to disable the listbox when my checkbox is checked. I can't understand why it doesn’t work. Can someone point out what's wrong in my code please?

此脚本在同一.cshtml页面中,而不在.js文件中:

This script is in the same .cshtml page and not in the .js file:

<script >

    $(function () {
        $('#allUsersCheck').click(function () {
            $('#lbUsers').prop("disabled", $(this).prop('checked'));
        });
    });

</script>

推荐答案

因为您使用的是"#allUsersCheck"的ID选择器.与您的列表框(?)不同,该复选框没有Id属性.

Because you are using the Id selector of '#allUsersCheck'. The checkbox does not have an Id attribute unlike your list box(?).

@Html.CheckBoxFor(m => m.allUsers, new { Name = "allUsersCheck" })

使用Id属性尝试以下操作:

Try the following with an Id attribute:

@Html.CheckBoxFor(m => m.allUsers, new { Name = "allUsersCheck", Id = "allUsersCheck" })

这篇关于复选框以启用或禁用列表框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆