简体   繁体   English

用jQuery隐藏div

[英]Hiding div with jquery

I'm using the following code to hide the text based on the answer given. 我正在使用以下代码根据给出的答案隐藏文本。 Its so simple but i must be missing something. 它是如此简单,但我必须缺少一些东西。 What am I doing wrong? 我究竟做错了什么?

$('.nameclass').on('change', function() {
        if( $('input[name=idname]').val() == '1' ) {
        $('#idname').removeClass('hide');
        } else {
            $('#idname').addClass('hide');
        }
    });


        <div class="hide" id="idname"> 
             <legend>To be hidden depending on answer</legend>
        </div>
    </div> 

Your select selector is not correct. 您的select器不正确。 In the listener selector, there is no tenaamstellingclass assigned class to the select box. 在侦听器选择器中,没有tenaamstellingclass分配给选择框的类。 In the if statement you are trying to get the select box by using input[name=tenaamstelling] when you should use it like select[name=tenaamstelling] because it is a select box and not an input field. 在if语句中,当您应该像select[name=tenaamstelling]这样使用它时,您尝试通过使用input[name=tenaamstelling]获得选择框,因为它是一个选择框,而不是输入字段。

You can either add the tenaamstellingclass class to your select input, or use this code. 您可以将tenaamstellingclass类添加到您select输入中,或使用此代码。

Just replace this: 只需替换为:

$('.tenaamstellingclass').on('change', function() {
    if( $('input[name=tenaamstelling]').val() == 'VOLMACHT' ) {
    $('#tenaamstelling_volmacht').removeClass('hide');
    } else {
        $('#tenaamstelling_volmacht').addClass('hide');
    }
});

with: 与:

$('select[name="tenaamstelling"]').on('change', function() {
    if( $(this).val() == 'VOLMACHT' ) {
        $('#tenaamstelling_volmacht').removeClass('hide');
    } else {
        $('#tenaamstelling_volmacht').addClass('hide');
    }
});

Now, if the .hide is defined correctly in your CSS, everything should be fine 现在,如果您在CSS中正确定义了.hide ,那么一切都会很好

You can check it here: 您可以在这里检查:

https://jsfiddle.net/vk3o6gyc/ https://jsfiddle.net/vk3o6gyc/

Given your HTML, the default for the #tenaamstelling_volmacht div is hidden and when option 2 is selected it is then displayed. 给定您的HTML, #tenaamstelling_volmacht div的默认设置将被隐藏,并在选择选项2时显示。

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

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