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条)

报告相同问题?

悬赏问题

  • ¥15 matlab中使用gurobi时报错
  • ¥15 WPF 大屏看板表格背景图片设置
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂