2017-09-23 5 views
1

選択されているラジオボタンをすべて確認できます。 しかし、レンダリングされたもの(「display:none」を持たないもの)をチェックしたいだけです。Jquery/Javascriptですべてのラジオボタン(レンダリングされている)が選択されているかどうかをチェックする方法

したがって、1と3の除算のみが選択されている場合は、真を表示する必要があります。現在、すべて3が選択されている場合にのみ、trueを表示します。

EDIT :私はShree33の回答をとり、input:radio:visibleで動作させました。

$(document).ready(function() { 
 
    $("a").click(function(e) { 
 
    e.preventDefault(); 
 
    var all_answered = true; 
 
    $(".division input:radio:visible").each(function() { 
 
     var name = $(this).attr("name"); 
 
     if ($("input:radio[name=" + name + "]:checked").length == 0) { 
 
     all_answered = false; 
 
     } 
 
    }); 
 
    alert(all_answered); 
 
    }) 
 
});
.test{ 
 
    //display:none; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<form> 
 
    <div class="division">1 
 
    <input type="radio" name="radio1" value="false" /> 
 
    <input type="radio" name="radio1" value="true" /> 
 
    </div> 
 
    
 
<div class="division test">2 
 
    <input type="radio" name="radio2" value="false" /> 
 
    <input type="radio" name="radio2" value="true" /> 
 
    </div> 
 
    
 
    <div class="division">3 
 
    <input type="radio" name="radio3" value="false" /> 
 
    <input type="radio" name="radio3" value="true" /> 
 
    </div> 
 
    <div>4 
 
    <input type="radio" name="radio4" value="false" /> 
 
    <input type="radio" name="radio4" value="true" /> 
 
    </div> 
 

 
</form> 
 
<a href="#">click</a>

+1

時間が経つにつれてそのリンクが死んでしまう可能性があるため、サードパーティのサイトにはリンクしないでください。質問に「コードスニペット」を挿入するだけです。 –

答えて

1

あなたは近くにいたので、$("input:radio")セレクタを$("input:radio:visible")に変更してください。それはうまくいくはずです。

$(document).ready(function() { 
 
    $("a").click(function(e) { 
 
    e.preventDefault(); 
 
    var all_answered = true; 
 
    $("input:radio:visible").each(function() { 
 
     var name = $(this).attr("name"); 
 
     if ($("input:radio[name=" + name + "]:checked").length == 0) { 
 
     all_answered = false; 
 
     } 
 
    }); 
 
    alert(all_answered); 
 
    }) 
 
});
.test{ 
 
    display:none; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<form> 
 
    <div class="division">1 
 
    <input type="radio" name="radio1" value="false" /> 
 
    <input type="radio" name="radio1" value="true" /> 
 
    </div> 
 
    
 
<div class="division test">2 
 
    <input type="radio" name="radio2" value="false" /> 
 
    <input type="radio" name="radio2" value="true" /> 
 
    </div> 
 
    
 
    <div class="division">3 
 
    <input type="radio" name="radio3" value="false" /> 
 
    <input type="radio" name="radio3" value="true" /> 
 
    </div> 
 

 
</form> 
 
<a href="#">click</a>

+0

jQueryセレクタをより正確にするにはどうすればよいですか? 私は、フォームのIDと特定のdivの たとえばを選択することができるようにしたい:$(「フォーム.division入力:ラジオ:目に見える」)。各(関数() これが機能していない –

0

あなたも親の目に見える状態を確認することができます。

if (($("input:radio[name=" + name + "]:first").parent().is(':visible')) && 
     ($("input:radio[name=" + name + "]:checked").length == 0)) { 
    all_answered = false; 
} 

https://jsfiddle.net/StepBaro/bLp8wbnh/3/

+0

サードパーティのサイトにリンクしないでください。これらのリンクは時間が経つと死に至る可能性があります。質問に「コードスニペット」を挿入するだけです。 –

+0

@ScottMarcus申し訳ありませんがスコット、私は同意します。リンクが死ぬことは事実ですが、私が書くのであれば、答えに必要なコードだけが読みやすくなります。あなたの編集では、私の編集内容が見えなくなります。また、私はStackOverflowシステムが好きではありません!私はJSFiddleを好む。 – Baro

+1

SOシステムが気に入らないのは残念ですが、作業コードをどこか別の場所に置くと、回答を消費するのが難しくなります(リンクが消えた場合)。あなたの修正を最初に投稿してから、完全な作業コードを回答に含めることはできません。 –

2

ただ、非表示のものを除外し、セレクタを使用して量を比較します同じセット内のチェックされたラジオボタンの数に一致します(01を使用))。金額が同じ場合は、すべての表示可能なボタンが選択されています。

また、実際にナビゲートしていないときにはリンクを使用しないでください。いくつかのコードをトリガする必要がある場合(ここの場合のように)、ちょうどどの要素にもclickイベントハンドラがバインドされています。 aを使用しない場合は、リンクのネイティブ動作(evt.preventDefault())と支援技術に依存する動作をキャンセルする必要はありません。たとえば、スクリーンリーダーでスクリーンリーダーが '実際にナビゲートします。あなたは長さを取得している場合は

$(function() { 
 
    $("#click").click(function(e) { 
 
    
 
    // Get only the visible DIVs that have the "division" class 
 
    var visibleDIVs = $("div.division:not(.hide)"); 
 
    
 
    // Now that we have a collection that contains only the div elements 
 
    // that are visible, we can get the count of them easily with: visibleDIVs.length 
 
    
 
    // We can also search the document for any checked radio buttons, but only those 
 
    // that are part of the visible divs collection like this: $("input:radio:checked", visibleDIVs). 
 
    // (the second argument (, visibleDIVs) constrains the search for radio buttons to just 
 
    // the collection of visilbe divs we've already gotten) and once we have those, 
 
    // we can also get the count of them by checking the .length of that collection. 
 
    
 
    // If the count of visible divs (visibleDIVs.length) equals the count of the visible 
 
    // checked radio buttons, then all buttons have been checked: 
 
    if(visibleDIVs.length === $("input:radio:checked", visibleDIVs).length){ 
 
     alert("All Answered"); 
 
    } 
 
    
 
    }) 
 
});
/* Make the clickable div look like a link */ 
 
#click { 
 
    text-decoration:underline; 
 
    cursor:pointer; 
 
} 
 

 
.hide { display:none; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<form> 
 
       <div class="division">1 
 
       <input type="radio" name="radio1" value="false"> 
 
       <input type="radio" name="radio1" value="true"> 
 
      </div> 
 

 
       <div class="division hide">2 
 
       <input type="radio" name="radio2" value="false"> 
 
       <input type="radio" name="radio2" value="true"> 
 
       </div> 
 

 
       <div class="division">3 
 
       <input type="radio" name="radio3" value="false"> 
 
       <input type="radio" name="radio3" value="true"> 
 
       </div> 
 

 
     </form> 
 
     <div id="click">click</div>

+0

おかげ : 場合(visibleDIVs.length === $( "入力:ラジオ:チェックする"、visibleDIVs).LENGTH) 私はかなり後の第2の部分を理解していないです===符号。 どのように2つの異なる要素の長さを取得していますか? –

+0

@LarryLiu [OK]を、コードのコメントをより良い説明を確認します。 –

+0

完璧な理解。スコットありがとう。 jQueryセレクタでより正確な情報を読むことができるリソースを紹介してください。たとえば、次のようになります。 var visibleDIVs = $( "div.division:not(.hide)"); 私は複数のフォームを持つので、どの親クラス/ IDを指定するかを指定したいと思います。 –

0

if ($("input:radio[name=" + name + "]:checked").length == 0) { 

if ($("input:radio[name=" + name + "]:checked").length == 0 && $(this).is(":visible") { 

を試してみてください、あなたが探しているものということですか?また、名前を取得して連結する必要がありますか?$(これは)あなたのオブジェクトも取得しません。

-1

PLSがthisを見てみましょう。 window.getComputedStyleを使用して「見える場合」の問題を解決しているようです。

+0

これではありません。それはコメントでなければなりません。 –

関連する問題