2017-02-08 27 views
0

我正在使用Joomla的新子表單代碼。這允許用戶複製一組輸入並重新使用它們。基本上是一個可重複的形式。 此表單創建以下結構。使用父類並獲得子類的中間部分

<div class="subform-repeatable-group"> 
    <div class="control-group jform_params__content__content0__dc_subheader-cg"></div> 
    <div class="control-group jform_params__content__content0__dc_typeofcontent-cg"></div> 
    <div class="control-group jform_params__content__content0__dc_toc_image-cg"></div> 
</div> 

問題是SubForm在父窗體內部加載,但Joomla將它視爲獨立窗體。因此正常的顯示/隱藏功能不再起作用。所以我必須創建自己的。

什麼我有什麼不好

這是生成的選擇:

<select id="jform_params_theme_selection" name="jform[params][theme_selection]" class="chzn-done"> 
    <option value="3dperspective" selected="selected">3D Perspective</option> 
    <option value="default">Default</option> 
    <option value="notheme">Select a theme!</option> 
</select> 

我已經得到了一段代碼,如果選擇了父窗體上的選擇值,將檢查。

$('#jform_params_theme_selection').bind('change', function (e) { 
    if($('#jform_params_theme_selection').val() == 'notheme') { 
    } else if($('#jform_params_theme_selection').val() == 'default') { 
    } else if($('#jform_params_theme_selection').val() == '3dperspective') { 
    } 
}).trigger('change'); 

現在我可以offcourse每個元素手動添加這樣的:

$('[class$="__dc_typeofcontent-cg"]').hide(); 

但是,必須有一個更好的辦法。

我想要什麼。

  1. 我想用.hide()功能上具有以下字母/符號在其__dc_類和有每個項目的.parents(.subform-repeatable-group)

一些額外的信息

  1. 還有另一種主題沒有__dc_,但被稱爲__threed_,所以我必須能夠定義字母/符號。
  2. 我已經檢查過我是否可以使用X數量的位置從前面或從後面X位置的東西,但這是不斷變化。

謝謝大家的幫助。

像往常一樣,我會繼續搜索和更新這篇文章,只要我得到更多的結果。

+1

請提供[mcve]。沒有任何表單控件顯示更改事件可以使用或者具有值 – charlietfl

+0

@charlietfl我添加了select,但我不認爲這很重要。因爲'change function already works',我只需要一個更好的方法來隱藏/顯示大量的控制組,因爲它們在Joomla中被調用。我需要將它與父類結合使用,因此它不會隱藏其他選項卡上的項目。我無法上傳所有的代碼,因爲如果我這樣做,我100%確定下一個評論是隻上傳準確指出問題的代碼。 – purple11111

回答

1

能使用filter()類似:

var cGroups = $('.subform-repeatable-group .control-group'); 

    var hideTheme = '_dc'; 
    var showTheme = '_threed'; 

    cGroups.filter(function(){ 
    return this.className.indexOf(hideTheme)>-1 
    }).hide() 



    cGroups.filter(function(){ 
    return this.className.indexOf(showTheme)>-1 
    }).show() 

DEMO

+0

這不需要我爲每個輸入添加這個。沒有'_typeofcontent-cg'的方法嗎?只有這個主題有大約100多個輸入。 這個工作: $('。subform-repeatable-group [class $ =''+ theme +'「]')。each()。hide(); – purple11111

+0

根據[mcve]顯示更多html。 *「這會工作嗎」* ...你嘗試過嗎? – charlietfl

+0

你還需要什麼HTML?請告訴我,因爲我不明白你現在失蹤了什麼? – purple11111

0

你的問題是有點令人費解,所以我專注於你想要的部分。

假設您對Joomla中拋出的類沒有太多控制,無論您需要構建捕獲父類和要查找的子子字符串的方法。但假設你有這兩個,你可以讓節目隱藏一點。您可以隨時告訴jQ在父代中查找子字符串的子代。

$("[class*='"+searchclass+"']",parent) 

爲您小提琴:https://jsfiddle.net/dvdxt58f/1/

0

最有效的方法是通過一個建議charlietfl但還有另一種方式來解決這個問題。

(function ($) { 
    $(document).ready(function() { 

     $('#jform_params_theme_selection').bind('change', function (e) { 
     if($('#jform_params_theme_selection').val() == 'notheme') { 
     } else if($('#jform_params_theme_selection').val() == 'default') { 

     $(".subform-repeatable-group div[class*='__threed_']").hide(); 
     $(".subform-repeatable-group div[class*='__dc_']").show();   

     } else if($('#jform_params_theme_selection').val() == '3dperspective') { 

     $(".subform-repeatable-group div[class*='__threed_']").show(); 
     $(".subform-repeatable-group div[class*='__dc_']").hide();   

     } 
    }).trigger('change'); 

    }); 
})(jQuery);  

基本上你使用*選擇與您創建:

$(".subform-repeatable-group div[class*='__threed_']").hide();

我加入這一個,因爲它可能在某些情況下,甚至當我與charlietfl的答案去是有用的。

演示:https://jsfiddle.net/tdo9go2q/11/