简体   繁体   中英

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

在此处输入图像描述

As you can see from my example, i select all radio box then sum it and put result into <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 :

 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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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