donglu953744 2017-07-28 15:29
浏览 35
已采纳

使用jQuery和ACF显示/隐藏Div

I have a dropdown list with a default and additional 3 options. I want to hide and show the divs based on the selection. If the selection is default all the divs should be visible. Otherwise based on the selection, the divs should show or hide. However my jQuery code doesn't seem to work. I have no clue why. Here is the code.

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js">. </script>
<script>
    $(document).ready(function(){
        $('#purpose').on('change', function() {
            if ( this.value == '1')
            {
                $(".partition").show();
            }
            else
            {
                $(".partition").hide();
            }
        });
    });
</script>

And the html code is

<select id="purpose" style="width:33%;height:50px;border:0px;background: #dadada; font-size:x-large;">
    <option selected><b>Type de matériel</b></option>
    <option value="1">Poster</option>
    <option value="2">Books</option>
    <option value="3">Audio</option>
</select>
<div class="partition">
    <?php the_field('poster_heading_1'); ?>
    <?php 
        $image = get_sub_field('partition_image');
        if( !empty($image) ):
            ?>
                <a href="<?php the_sub_field('partition_pdf'); ?>" target="_blank"> 
                    <img src="<?php echo $image['url']; ?>">
                </a>
            <?php  the_sub_field('partition_text'); ?>
        <?php endif; ?>
</div>
  • 写回答

2条回答 默认 最新

  • dongwen9947 2017-07-28 15:52
    关注

    If I understood correctly, you should have 3 partition div to work like you said, each div should have the id number equals select option value that you want to show. Check this code and let me say if this is what you need.

    jQuery:

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js">. </script>
    <script>
        $(document).ready(function(){ 
            $('#purpose').on('change', function() { 
                var val = $(this).val();
                console.log(val);
                if ( val == '0' ) { 
                    $(".partition").show(); 
                } else { 
                    $(".partition").hide(); 
                    $("#partition-" + val).show(); 
                } 
            }); 
        }); 
    </script>
    

    HTML:

    <select id="purpose" style="width:33%;height:50px;border:0px;background: #dadada; font-size:x-large;">
        <option selected value="0"><b>Type de matériel</b></option>
        <option value="1">Poster</option>
        <option value="2">Books</option>
        <option value="3">Audio</option>
    
    </select>
    
    
    <div class="partition" id="partition-1">
        <?php the_field('poster_heading_1'); ?>
        <?php 
        $image = get_sub_field('partition_image');
        if( !empty($image) ):
            ?>
        <a href="<?php the_sub_field('partition_pdf'); ?>" target="_blank"> <img src="<?php echo $image['url']; ?>"> </a>
        <?php  the_sub_field('partition_text'); ?>
    <?php endif; ?>
    </div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题
  • ¥15 Python时间序列如何拟合疏系数模型