2016-11-14 30 views
1

我想知道如何修改一個函數,該函數採用類名+附加數字並應用於具有相應數字的另一個類。在JS函數參數和目標中自動增加附加類或ID號

我有地方現在是:

function hideSect() 
{ 
    if($('.trigger').is(":checked")) 
     $(".condition").hide(); 
    else 
     $(".condition").show(); 
} 

我將如何修改它,以便它可以動態地採取TRIGGER1,並適用於條件1,觸發2到條件2等

相關HTML:

<input type="checkbox" class="trigger" onchange="hideSect()"><label><span>I do not wish to furnish this information.</span></label> 
<div class="condition"> 
    <!--Conditional content in here --> 
</div> 
+0

什麼是HTML佈局會是什麼樣子?可能會更簡單。 – epascarello

+0

您是否擁有對HTML的控制權?你可以發佈嗎? – tymeJV

+0

更新了相關html的問題 –

回答

0

隨着該HTML佈局,你可以使用CSS

input.trigger:checked + label + div { 
 
    display: none; 
 
}
<input type="checkbox" class="trigger" > 
 
<label><span>I do not wish to furnish this information.</span> 
 
</label> 
 
<div class="condition"> 
 
    <p>Hello</p> 
 
    <!--Conditional content in here --> 
 
</div> 
 
<br/> 
 
<input type="checkbox" class="trigger" > 
 
<label><span>I do not wish to furnish this information.</span> 
 
</label> 
 
<div class="condition"> 
 
    <p>Hello</p> 
 
    <!--Conditional content in here --> 
 
</div>

以外,你可以只發現有類條件的下一個同級。

$(".trigger").on("change", function(){ 
 
    $(this).nextAll(".condition").first().toggle(!this.checked); 
 
    
 
}).change();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> 
 
<input type="checkbox" class="trigger" > 
 
<label><span>I do not wish to furnish this information.</span> 
 
</label> 
 
<div class="condition"> 
 
    <p>Hello</p> 
 
    <!--Conditional content in here --> 
 
</div> 
 
<br/> 
 
<input type="checkbox" class="trigger" > 
 
<label><span>I do not wish to furnish this information.</span> 
 
</label> 
 
<div class="condition"> 
 
    <p>Hello</p> 
 
    <!--Conditional content in here --> 
 
</div>

如果你不能依靠HTML佈局,除了使用數據屬性

$(".trigger").on("change", function(){ 
 
    var cb = $(this), 
 
     selector = cb.data("toggles"); 
 
    $(selector).first().toggle(!this.checked); 
 
    
 
}).change();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> 
 
<input type="checkbox" class="trigger" data-toggles="#f1" > 
 
<label><span>I do not wish to furnish this information.</span> 
 
</label> 
 
<div class="condition" id="f1"> 
 
    <p>Hello</p> 
 
    <!--Conditional content in here --> 
 
</div> 
 
<br/> 
 
<input type="checkbox" class="trigger" data-toggles="#f2" > 
 
<label><span>I do not wish to furnish this information.</span> 
 
</label> 
 
<div class="condition" id="f2"> 
 
    <p>Hello</p> 
 
    <!--Conditional content in here --> 
 
</div>

如果你真的需要使用IDS,你可以做點像

$(".trigger").on("change", function(){ 
 
    var num = this.id.match(/\d+$/)[0]; 
 
    $("#f" + num).toggle(!this.checked); 
 
}).change();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> 
 
<input type="checkbox" class="trigger" ID="c1" > 
 
<label><span>I do not wish to furnish this information.</span> 
 
</label> 
 
<div class="condition" id="f1"> 
 
    <p>Hello</p> 
 
    <!--Conditional content in here --> 
 
</div> 
 
<br/> 
 
<input type="checkbox" class="trigger" id="c2" > 
 
<label><span>I do not wish to furnish this information.</span> 
 
</label> 
 
<div class="condition" id="f2"> 
 
    <p>Hello</p> 
 
    <!--Conditional content in here --> 
 
</div>

+0

感謝您的回覆,我可能遇到的一個小問題是,有時條件不是觸發器旁邊。此代碼是我製作的多頁表單的一部分。例如,有時我會遇到第1頁上的是/否問題將決定是否顯示第5頁上的問題的情況。或者即使第6頁將顯示 –

+0

所以我會使用數據屬性,而不是從ID分析數字。 – epascarello

+0

使用其他選項編輯。 – epascarello