简体   繁体   English

如何检查是否选择了在数据网格内部动态创建的单选按钮?

[英]How to check whether a radio button is selected which is created inside a data grid dynamically?

I have a data grid which is populated at runtime with some values. 我有一个在运行时填充一些值的数据网格。 The first two columns are for radio buttons with 1st column containing 'Yes' and second being 'No' . 前两列用于单选按钮,第一列包含“是”,第二列包含“否”。 There are n number of rows in the data grid. 数据网格中有n行。 If even one 'Yes' is selected a text box should appear. 如果选择了“是”,则将出现一个文本框。 How do I achieve this? 我该如何实现?

I tried it by using 我尝试使用

$(".class").each(function () {
    if ($(this).checked) {
        textbox.visible = true;
    }
});

But $(this).checked is returning false 但是$(this).checked返回false

you can try with prop which returns true/false based on weather it is checked or not. 您可以尝试使用prop ,它会根据是否检查天气返回true / false。

$(".class").each(function() {
   if( $(this).prop('checked') ) 
   {  
      textbox.visible = true; 
   }
});

The snippet below will answer your question 以下代码段将回答您的问题

 $('.class').click(function(){ $(".class").each(function() { if($(this).is(':checked') && $(this).val()=='yes' ) { console.log($(this).prop('name') + ' checked'); } }); }); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <table > <tr> <td>Yes</td><td>No</td> </tr> <tr> <td><input name='question1' value='yes' type='radio' class='class'/></td><td><input name='question1' type='radio' class='class'/></td> </tr> <tr> <td><input name='question2' value='yes' type='radio' class='class'/></td><td><input name='question2' type='radio' class='class'/></td> </tr> <tr> <td><input name='question3' value='yes' type='radio' class='class'/></td><td><input name='question3' type='radio' class='class'/></td> </tr> </table> 

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

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