简体   繁体   English

使用jQuery根据复选框值禁用Div内容

[英]Disable Div Contents Based on checkbox value using jquery

hi am developing mvc project using c# 嗨,我正在使用C#开发MVC项目

currently am working on employee system model 目前正在研究员工系统模型

I have designed view below like this 我这样设计了下面的视图

 @Html.CheckBoxFor("value", new {data_divToDisable="SSLCSection",@class="SelectSection" })
                  <div id="SSLCSection" class="DisableDiv">
<table>
//Conntent goes here
</table>
</div>


@Html.CheckBoxFor("value", new {data_divToDisable="PUCSection",@class="SelectSection" })
                  <div id="PUCSection" class="DisableDiv">
<table>
//Conntent goes here

</table>
</div>

@Html.CheckBoxFor("value", new {data_divToDisable="GraduationSection",@class="SelectSection" })
                  <div id="GraduationSection" class="DisableDiv">
<table>
//Conntent goes here

</table>
</div>
@Html.CheckBoxFor("value", new {data_divToDisable="PostGraduationSection",@class="SelectSection" })
                  <div id="PostGraduationSection" class="DisableDiv">
<table>
//Conntent goes here

</table>
</div>

here I need to disable sections when loading view based on checkbox value 在这里我需要基于复选框值加载视图时禁用部分

like if checkbox is checked no need to disable that section otherwise it would be disable 例如,如果复选框已选中,则无需禁用该部分,否则将被禁用

I have written jquery like this 我写了这样的jQuery

 $(document).ready(function () {
     $(".SelectSection").attr('checked', false,function(){
            var id = $(this).find(".DisableDiv");
            alert(id);
           $(this).find(".DisableDiv").find('input, textarea, select,table').each(function () {
                $(this).prop('disabled', true);
            });
        });

this is not doing anything for us 这对我们没有任何帮助

please help and your help would be greately appreciated 请帮助,非常感谢您的帮助

Please Note I have using data-Attribute to simplify the jquery and while each time loading the page it has to disable the sections based on checkbox value (hint:it has to disable if checkbox value is false ) 请注意,我已经使用data-Attribute简化了jquery,并且每次加载页面时,它都必须根据复选框值禁用部分(提示:如果复选框值为false,则必须禁用部分)

You could try the follow, excuse the pseudo code. 您可以尝试以下操作,请原谅伪代码。

HTML 的HTML

<input class="SelectSection" type="checkbox" >hello1</input>

<div id="SSLCSection" class="DisableDiv" >
     <input type="input">Test</input>
    <textarea>Test1</textarea>
</div>


    <br>
<input class="SelectSection" type="checkbox">hello2</input>
<div id="PUCSection" class="DisableDiv" >
     <input type="input">Test2</input>
    <textarea>Test12</textarea>
</div>


<br>
<input class="SelectSection" type="checkbox" checked>hello3</input>
<div id="PostGraduationSection" class="DisableDiv" >
     <input type="input">Test3</input>
    <textarea>Test3</textarea>
</div>

jQuery jQuery的

$(document).ready(function () {
$(".SelectSection[type='checkbox']").each(function (i, o) {
    if ($(o).is(":checked")) {

        $(o).next('div').find('input,textarea').each(function (i, o) {
            $(o).prop('disabled', true);
        });

    }
});
})

The above assumes that your next element after the relevant checkbox is the div in question which holds the elements that need to be disabled. 上面假设相关复选框之后的下一个元素是有问题的div,其中包含需要禁用的元素。

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

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