如果在jquery中检查单选按钮,我如何隐藏和显示div

时间:2015-01-08 22:32:24

标签: jquery wordpress

在wordpress中我为帖子格式创建了一些元文件,所以当用户点击一个单选按钮或已经选中元数据显示时。

这里是我尝试了一些jquery的代码,但我对jquery不太好

HTML:

<div id="post-formats-select">
    <input type="radio" id="post-format-0" class="post-format" value="0" name="post_format" />
    <label class="post-format-standard">Standard</label>

    <input type="radio" id="post-format-chat" class="post-format" value="chat" name="post_format" />
    <label class="post-format-chat">Chat</label>

    <input type="radio" id="post-format-gallery" class="post-format" value="gallery" name="post_format" checked="checked" />
    <label class="post-format-gallery">Gallery</label>

    <input type="radio" id="post-format-video" class="post-format" value="video" name="post_format" />
    <label class="post-format-video">Video</label>    
</div>

<div class="meta-box-sortables">
    <div id="inpost_chat_box" class="postbox">
        Chat Box
    </div>    

    <div id="inpost_gallery_box" class="postbox">
        Gallery Box
    </div>

    <div id="inpost_video_box" class="postbox">
        Video Box
    </div>    
</div>

JQuery的:

$('#inpost_chat_box, #inpost_gallery_box, #inpost_video_box').hide();

var formats = $('#post-formats-select input');
formats.on('change', function(){
    if( $(this).is(':checked').val() == 'gallery' ) {
        $('#inpost_gallery_box').show();
    }
});

1 个答案:

答案 0 :(得分:1)

你不能链.is(),因为它返回一个布尔值而不是jQuery对象。 .val()内没有.is()方法,因此它可能会抛出:

  

未捕获的ReferenceError:undefined不是函数

如果您查看浏览器的控制台。

尝试将条件分解为两个语句,如下所示:

formats.on('change', function(){
    if( this.checked && this.value == 'gallery' ) {
        $('#inpost_gallery_box').show();
    }
});