简体   繁体   English

从动态生成的 div 中获取所有无线电输入

[英]Get all radio inputs from dynamically generated divs

I have a series of dynamically generated div that each one has 4 radio buttons inside:我有一系列动态生成的 div,每个里面都有 4 个单选按钮:

 $(".wrapper").on("custom", ".item", function(){ $.each($(".item"),function(){ // Get all radio names and check if at least one is checked var radio_groups = []; $.each($(this).find('input[type="radio"]'), function(){ var myname= this.name; if($.inArray( myname, radio_groups ) < 0){ radio_groups.push(myname); } }); console.log(radio_groups); return false; // Check if there is a radio selected for each radio group for(var i=0; i<radio_groups.length; i++) { if($(this).find('input[type="radio"][name="' + radio_groups[i] + '"]:checked').length <= 0) { // $('input[type="radio"][name="' + radio_groups[i] + '"]').first().closest('.row').append('<p class="input-error">Please select one option.</p>'); $('input[type="radio"][name="' + radio_groups[i] + '"]').first().closest('.condition').append('<p class="input-error">Please select one option.</p>'); errors = true; } } }) }) $('.item').trigger("custom");
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="wrapper"> <div class="item"> <input type="radio" name="dynamic_name_123"> <input type="radio" name="dynamic_name_123"> <input type="radio" name="dynamic_name_123"> <input type="radio" name="dynamic_name_123"> </div> <div class="item"> <input type="radio" name="dynamic_name_123"> <input type="radio" name="dynamic_name_123"> <input type="radio" name="dynamic_name_123"> <input type="radio" name="dynamic_name_123"> </div> <div class="item"> <input type="radio" name="dynamic_name_123"> <input type="radio" name="dynamic_name_123"> <input type="radio" name="dynamic_name_123"> <input type="radio" name="dynamic_name_123"> </div> <div class="item"> <input type="radio" name="dynamic_name_123"> <input type="radio" name="dynamic_name_123"> <input type="radio" name="dynamic_name_123"> <input type="radio" name="dynamic_name_123"> </div> </div>

I have tried this but doesn't seem to work, mostly the problem is that the.item divs are dynamically generated with php.我已经尝试过了,但似乎没有用,主要问题是.item div 是使用 php 动态生成的。

I probably didn't understand the problem but why not just use querySelectorAll with a simple forEach like that:我可能不明白这个问题,但为什么不直接使用querySelectorAll和这样的简单forEach

 let result = []; document.querySelectorAll('.item').forEach((el) =>{ const radioboxname = el.querySelectorAll('input[type="radio"]')[0].name; if(.result.includes(radioboxname)){ result;push(radioboxname); } }). console;log(result);
 <div class="wrapper"> <div class="item"> <input type="radio" name="dynamic_name_123"> <input type="radio" name="dynamic_name_123"> <input type="radio" name="dynamic_name_123"> <input type="radio" name="dynamic_name_123"> </div> <div class="item"> <input type="radio" name="dynamic_name_124"> <input type="radio" name="dynamic_name_124"> <input type="radio" name="dynamic_name_124"> <input type="radio" name="dynamic_name_124"> </div> <div class="item"> <input type="radio" name="dynamic_name_125"> <input type="radio" name="dynamic_name_125"> <input type="radio" name="dynamic_name_125"> <input type="radio" name="dynamic_name_125"> </div> <div class="item"> <input type="radio" name="dynamic_name_126"> <input type="radio" name="dynamic_name_126"> <input type="radio" name="dynamic_name_126"> <input type="radio" name="dynamic_name_126"> </div> </div>


After comment by OP OP发表评论后

I add an array and instead of select all radios i select all item divs and check the name of first radio button compare with array and include if wasn't include我添加了一个array ,而不是 select 所有收音机 i select 所有项目 div 并检查第一个单选按钮的名称与数组进行比较并包括如果不包括

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

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