2012-08-13 245 views
0

我遇到了hide_fields()函数的问题。我试图隐藏所有'行布局字段'<div> -s,然后显示与<select>在同一父()父()中的某些div。仅隐藏父元素的子元素

根据您从<select>中选择的内容,应显示某些字段。

有几个这样的行,问题是,如果我有3行,当我从<select>-s中选择选项时,它会隐藏所有字段。

代码粘贴在下面。

标记是利用ACF WordPress的管理,所以它的相当激烈,但是,如果你需要看它,并可以在这里找到:http://pastie.org/4467255

(function($) { 

    // hide_fields/a small function to hide all the conditional fields 
    function hide_fields() { 
     $('#acf-content_repeater table tbody tr.row td div.row-layout-field:nth-child(1n+2)').hide(); 
    } 

    // Document Ready 
    $(document).ready(function() { 

     // hide all fields 
     hide_fields(); 

     // trigger change on the select field to show selected field 
     $('#acf-content_repeater table tbody tr.row td div.row-layout-field select').trigger('change'); 

    }); 

    // Hero Type change 
    $('#acf-content_repeater table tbody tr.row td div.row-layout-field select').live('change', function() { 

     // vars 
     var value = $(this).val(); 

     // hide all fields 
     hide_fields(); 

     // show the selected field 
     if(value == "image") { 
      // console.log($(this).parent().parent().find('div.row-layout-field:nth-child(2)')); 
      $(this).parent().parent().find('div.row-layout-field:nth-child(2)').show(); 
      $(this).parent().parent().find('div.row-layout-field:nth-child(3)').show(); 
      $(this).parent().parent().find('div.row-layout-field:nth-child(4)').show(); 
     } 
     else if(value == "video") { 
      $(this).parent().parent().find('div.row-layout-field:nth-child(4)').show(); 
      $(this).parent().parent().find('div.row-layout-field:nth-child(5)').show(); 
     } 
     else if(value == "tweet") { 
      $(this).parent().parent().find('div.row-layout-field:nth-child(5)').show(); 
      $(this).parent().parent().find('div.row-layout-field:nth-child(6)').show(); 
     } 
     else if(value == "statistic") { 
      $(this).parent().parent().find('div.row-layout-field:nth-child(6)').show(); 
      $(this).parent().parent().find('div.row-layout-field:nth-child(7)').show(); 
     } 

    }); 

})(jQuery); 

感谢您的任何援助。

编辑:

我成功地实现了我的这种需要:http://pastie.org/4467732

也许不是最优雅,但它的工作原理。

回答

0

我有点傻瓜,我将我的答案,我原来的职位,而不是回答我的问题.. 。这里是什么,是上面:

我成功地实现了我的这种需要:http://pastie.org/4467732

也许不是最优雅,但它的工作原理。

0

可以使用父隐藏元素的父具体的儿童和发现

$(this).parent().find('.row-layout-field').hide(); 
+0

我很感激帮助,但不幸的是,这似乎并不适合我。我在原始文章中添加了一个编辑,显示了我是如何实现我所需的。谢谢。 – beefchimi 2012-08-13 19:03:52