簡體   English   中英

如何從表中獲取選定單選按鈕的值,然后使用 jQuery 計算選定單選按鈕

[英]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>


您可能不需要 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