[英]How to get value of Selected radio button from table and then count selected radio button using jQuery
我想使用 jQuery 获得价值选择的单选按钮
从我的示例中可以看出,我 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>
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.