简体   繁体   中英

How to get selected radio value using Jquery

Hi i want to get radiobutton value using jquery. This is the radiobutton code. The period[count] naming is because the radiobutton is loop in array using javascript

<input type='radio' name='period["+count+"]' value='1' checked/>Full<input type='radio' name='period["+count+"]' value='0.5'/>Half (AM)<input type='radio' name='period["+count+"]' value='0.5'/>Half (PM)

Question is how do i get the total value of the checked radio button?I try this code but it give only the first value of checked radio button

var values2 = $("input:radio[name='period[2]']").val();
var values3 = $("input:radio[name='period[3]']").val();

Edited

This is the code to solve this problem thanks to Aaron Saunders

var x = 0;
    $("input:radio[name^='period']:checked").each(function(){
    x = x + parseFloat($(this).val());
    });

find all that start with period, then add them up

var x = 0;
$("input:radio[name^='period']").each(function{
        x = x + this.val();
     });

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