doom910730 2018-09-27 12:36
浏览 66

显示多个div

so, I'm working on a project that has 2 tables in database that are related!

I need via JavaScript Hide and Show the divs containing the right information.

I got a main product Stone, and each Stone has 2 other Stone Types

So if I click on the img that has Stone1, I want to show Stone1.1, Stone1.2, etc and hide all the other StoneTypes.

This is what I got now on JS

jQuery('.Pedras').click(function(){
          jQuery('.SubPedras').hide();
          jQuery('#div'+$(this).attr('target')).show();
    });

The problem here is that I only get the first div to show, eventhough I have 2 divs with the same Name example:

<div id="div{IdPedra}" class="SubPedras">
<div id="div{IdPedra}" class="SubPedras">
<div id="div{IdPedra}" class="SubPedras">

And {IdPedra} Is the id of the stone in the database, so I will get "div1" two or 3 times, etc.

Can anyone pls help me, I'm not able to find a suitable solution for my need.

Thank you!

  • 写回答

1条回答 默认 最新

  • douna6802 2018-09-27 12:59
    关注

    You could use data-attributes to accomplish this. In your change my code to;

    <div data-id="{IdPedra}" class="SubPedras">Stone 1.1</div>
    <div data-id="{IdPedra}" class="SubPedras">Stone 1.2</div>
    <div data-id="{IdPedra}" class="SubPedras">Stone 2.1</div>
    <div data-id="{IdPedra}" class="SubPedras">Stone 2.2</div>
    

    You can access an element's data-attribute by using $(element).data("name"); this is equivalent to data-name. I also stored the Pedras

    Loop through the SubPedras and check if they match the button where the parent category or value you wish to use as filter. Run the snippet, thanks!

    (You could also store them to class attribute instead of data)

    jQuery('.Pedras').click(function() {
      // store button value to variable
      var pedraValue = $(this).val();
      
      // loop through the SubPedras
      $(".SubPedras").each(function() {
        
        // get the value from data attribute
        if ($(this).data("id") == pedraValue) {
          
          // if match show
          $(this).show();
        }
        else{
          // else hide
          $(this).hide();
        }
      });
    });
    .SubPedras{
      line-height:40px;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <div data-id="Stone1" class="SubPedras">Stone 1.1</div>
    <div data-id="Stone1" class="SubPedras">Stone 1.2</div>
    <div data-id="Stone2" class="SubPedras">Stone 2.1</div>
    <div data-id="Stone2" class="SubPedras">Stone 2.2</div>
    
    <button class="Pedras" value="Stone1">Stone 1</button>
    <button class="Pedras" value="Stone2">Stone 2</button>

    </div>
    
    评论

报告相同问题?

悬赏问题

  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)