2016-11-14 4 views
1

クラス名+追加番号を受け取り、対応する番号の別のクラスに適用する方法を変更する方法が不思議でした。JS関数の関数名とターゲットの自動インクリメント付き追加クラスまたはID番号

私は今のところしているのは次のとおりです。

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

それが動的にTRIGGER1を取り、等の条件1、条件2にトリガ2、

関連HTMLに適用することができるように、私はそれを修正する方法を:その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

、あなただけ

0 CSSを使用することができますあなただけのクラスの条件を持って次の兄弟を見つけることができるそれ以外

データ属性

$(".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>

を使用するよりも、あなたは、HTMLレイアウトに頼ることができない場合は

$(".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>
そして、あなたが本当にIDを使用する必要がある場合は、あなたは何かをすることができます

$(".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

関連する問題