简体   繁体   English

同时div显示/隐藏和启用/禁用复选框检查/取消选中使用Javascript / jQuery

[英]Simultaneous div show/hide and enable/disable on checkbox check/uncheck using Javascript/jQuery

I have the following view: 我有以下观点:

<div>
    <label id = "show_hide_checkbox_label">Text Input?</label>
    <input type ="checkbox" class="myCheckbox" onchange="show_hide_divs()" />
</div>
<div id="text_div" hidden>
    <label id ="emp_id_input_label">Employee ID:</label>
    <input type="text" name="emp_id" id="employee_id_input"></input>
</div>
<div id="select_div">
     <label id= "emp_id_add_label">Employee ID:</label>
     <select name="emp_id" id = "employee_id">
     <option>--Select--</option>
     <option>INCB001</option>
     <option>INCB002</option>
     <option>INCB003</option>
     </select>
</div>

What I want to do is this: 我想要做的是:

By default, the checkbox is unchecked, the textbox is hidden and disabled and the select element is visible, and I can only post data from the options in the select element, I can not post data from textbox. 默认情况下,取消选中该复选框,隐藏和禁用文本框,并且select元素可见,我只能从select元素中的选项发布数据,我无法从文本框中发布数据。 But if I check the checkbox, then select element becomes hidden and disabled, and the textbox is visible and enabled, and I can only post data from textbox and not from the select element. 但是,如果我选中复选框,则选择元素将变为隐藏和禁用,文本框可见并启用,我只能从文本框而不是从select元素发布数据。 If I uncheck the checkbox again, the behavior is reversed and so on. 如果我再次取消选中该复选框,则反转行为,依此类推。

Unfortunately, the jQuery code I have below doesn't allow me to disable/enable and show/hide the divs properly on checkbox change. 不幸的是,我在下面的jQuery代码不允许我在复选框更改时禁用/启用和显示/隐藏div。

Here's my jQuery code: 这是我的jQuery代码:

    <script>
        function show_hide_divs()
        {
                if ($('.myCheckbox').is(':checked'))
                {
                    $("div#text_div").show();
                    $("div#text_div").children().prop('disabled', 'false');
                    $("div#select_div").hide();
                    $("div#select_div").children().prop('disabled', 'true');
                }
                else {
                     $("div#select_div").show();
                     $("div#select_div").children().prop('disabled', 'false');
                     $("div#text_div").hide();
                     $("div#text_div").children().prop('disabled', 'true');
                }
        }
    </script>

It seems there's something wrong with my jQuery code, only just I can't figure out myself. 我的jQuery代码似乎有问题,只是我无法弄清楚自己。

Chances are your current code is in a DOM ready handler so the function is effectively hidden in the scope of the wrapper function. 有可能你当前的代码在DOM就绪处理程序中,因此该函数有效地隐藏在包装函数的范围内。

Don't use attribute-based event handlers with jQuery. 不要将基于属性的事件处理程序与jQuery一起使用。 There is no point separating the event registration from the event handler code, when the jQuery version is more flexible (supports multiple handlers). 当jQuery版本更灵活(支持多个处理程序)时,将事件注册与事件处理程序代码分开是没有意义的。

Also, use boolean values for boolean prop settings. 另外,对布尔prop设置使用布尔值。 Seems to be a bug in some browsers if you use strings: 如果您使用字符串,似乎是某些浏览器中的错误:

http://jsfiddle.net/TrueBlueAussie/3me1og4o/2/ http://jsfiddle.net/TrueBlueAussie/3me1og4o/2/

$(function () {
    $('.myCheckbox').change(function () {
        if ($(this).is(':checked')) {
            $("div#text_div").show();
            $("div#text_div").children().prop('disabled', false);
            $("div#select_div").hide();
            $("div#select_div").children().prop('disabled', true);
        } else {
            $("div#select_div").show();
            $("div#select_div").children().prop('disabled', false);
            $("div#text_div").hide();
            $("div#text_div").children().prop('disabled', true);
        }
    });
});

It also means you can reference the checkbox clicked as this in the handler, which is also better as it leads to supporting multiple controls on the same page (you would then start making the other selectors relative to the clicked control). 这也意味着你可以参考checkbox单击作为this在处理程序,这是因为它会导致在同一页上支持多个控件(那么你就开始做相对点击控制的其他选择器)也比较好。

Note: $(function(){}): is just a handy shortcut for $(document).ready(function(){}); 注意: $(function(){}):只是$(document).ready(function(){});的便捷快捷方式$(document).ready(function(){});

Check this : http://jsfiddle.net/9cdoz896/2/ 检查一下: http//jsfiddle.net/9cdoz896/2/

JS JS

$('.myCheckbox').on('click',function(){
 if ($('.myCheckbox').is(':checked'))
            {
                $("div#text_div").show();
                $("div#text_div").children().prop('disabled', false);
                $("div#select_div").hide();
                $("div#select_div").children().prop('disabled', true);
            }
            else {
                 $("div#select_div").show();
                 $("div#select_div").children().prop('disabled', false);
                 $("div#text_div").hide();
                 $("div#text_div").children().prop('disabled', true);
            }
    });

CSS CSS

#text_div{
        display:none;    
}

HTML HTML

<div>
    <label id = "show_hide_checkbox_label">Text Input?</label>
    <input type ="checkbox" class="myCheckbox" />
</div>
<div id="text_div" >
    <label id ="emp_id_input_label">Employee ID:</label>
    <input type="text" name="emp_id" id="employee_id_input"></input>
</div>
<div id="select_div">
     <label id= "emp_id_add_label">Employee ID:</label>
     <select name="emp_id" id = "employee_id">
     <option>--Select--</option>
     <option>INCB001</option>
     <option>INCB002</option>
     <option>INCB003</option>
     </select>
</div>

Is the script tag included in your html itself? 脚本标签是否包含在您的html中? or show_hide_divs function visible to DOM(ie handler is registered after DOM is loaded). show_hide_divs函数对DOM可见(即在加载DOM后注册处理程序)。 If script is in same html then move script tag to end of body tag. 如果脚本在同一个html中,则将脚本标记移动到body标记的末尾。 If its in external file then remove <script> tag first. 如果它在外部文件中,则首先删除<script>标记。 Don't use inline event handler. 不要使用内联事件处理程序。 Here is fiddle: http://jsfiddle.net/mrk_m/g9Lo3u8r/1/ 这是小提琴: http//jsfiddle.net/mrk_m/g9Lo3u8r/1/
This fiddle also solves your problem of disabling elements permanently. 这个小提琴也解决了永久禁用元素的问题。 It uses removeProp and addProp. 它使用removeProp和addProp。

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

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