简体   繁体   English

如何从表中获取选定单选按钮的值,然后使用 jQuery 计算选定单选按钮

[英]How to get value of Selected radio button from table and then count selected radio button using jQuery

I want to get value selected radio button using jQuery我想使用 jQuery 获得价值选择的单选按钮

在此处输入图像描述

As you can see from my example, i select all radio box then sum it and put result into <p>从我的示例中可以看出,我 select 所有单选框然后将其相加并将结果放入<p>

 let total = 0; $('table input:radio:checked').each(function() { total += isNaN(parseInt($(this).val()))? 0: parseInt($(this).val()); }); $("p").html(total);
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <table> <tr> <td><input type='radio' value='1' checked>1</td> <td><input type='radio' value='2'>2</td> <td><input type='radio' value='3' checked>3</td> </tr> </table> total: <p></p>


You might not need jQuery : 您可能不需要 jQuery

 let total = 0; let radios = document.querySelectorAll('table input[type="radio"]:checked'); radios.forEach((radio)=>{ total += isNaN(parseInt(radio.value))? 0: parseInt(radio.value); }); document.querySelector('p').innerHTML = total;
 <table> <tr> <td><input type='radio' value='1' checked>1</td> <td><input type='radio' value='2'>2</td> <td><input type='radio' value='3' checked>3</td> </tr> </table> total: <p></p>

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

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