【问题标题】:How can i hide and show div if radio button checked in jquery如果在 jquery 中选中单选按钮,我如何隐藏和显示 div
【发布时间】:2015-01-08 22:32:23
【问题描述】:

在 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();
    }
});

【问题讨论】:

标签: jquery wordpress


【解决方案1】:

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

Uncaught ReferenceError: undefined is not a function

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-02-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-25
    • 1970-01-01
    • 2014-04-21
    • 1970-01-01
    相关资源
    最近更新 更多