简体   繁体   English

如何检查是否在Jquery / Javascript中选择了所有已渲染的单选按钮

[英]How to check if all radio buttons (that are rendered) are selected in Jquery/Javascript

I am able to check for all radio buttons that are selected. 我能够检查所有选中的单选按钮。 However ,I only want to check for those that are rendered (the ones that don't have "display:none"). 但是,我只想检查那些已渲染的对象(那些没有“ display:none”的对象)。

So if only the 1 and 3 division is selected, it should display true. 因此,如果仅选择1和3除法,则应显示true。 Currently, it will only display true if all 3 is selected. 当前,只有选择全部3个,它才会显示true。

EDIT : I have taken Shree33 answer and made it work with input:radio:visible . 编辑 :我已经采取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> 

Just use a selector that excludes the non-displayed ones and compare the amount of found elements to the amount of checked radio buttons in that same set (using JQuery context ). 只需使用一个选择器(不包括未显示的选择器),然后将找到的元素数量与同一集合中选中的单选按钮的数量进行比较(使用JQuery context )。 If the amounts are the same, all visible buttons have been selected. 如果数量相同,则已选择所有可见按钮。

Also, you really shouldn't use a link when you aren't actually navigating anywhere. 另外,当您实际上没有在任何地方导航时,实际上不应该使用链接。 If you just need to trigger some code (as is the case here), just about any element can have a click event handler bound to it. 如果只需要触发一些代码(如此处所示),则几乎任何元素都可以绑定有click事件处理程序。 By not using an a , you don't have to cancel the native behavior of the link ( evt.preventDefault() ) and those that rely on assistive technologies, like screen readers won't have problems that occur when the screen reader encounters a link that doesn't actually navigate. 通过不使用a ,您不必取消链接的本机行为( evt.preventDefault() ),而那些依赖于辅助技术的链接,例如屏幕阅读器,则不会遇到当屏幕阅读器遇到a时出现的问题。实际无法导航的链接。

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

You were close, just change the $("input:radio") selector to $("input:radio:visible") . 您已经接近,只需将$("input:radio")选择器更改$("input:radio:visible") That should work. 那应该工作。

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

You can check for the parent visible state too: 您也可以检查父级可见状态:

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/ https://jsfiddle.net/StepBaro/bLp8wbnh/3/

Where you're getting the length, 你要去的地方,

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

try 尝试

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

Is that what you are looking for? 那是您要找的东西吗? Also do you need to get the name and concat it, as won't $(this) get you your object as well? 另外,您还需要获取名称并连接它吗,因为$(this)不会也使您获得对象吗?

Pls have a look at this . 请看看这个 Seems to solve your "if visible" issue with window.getComputedStyle. 似乎可以解决window.getComputedStyle的“如果可见”问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM