单选按钮的值求和功能 [英] values from radio buttons sum function

查看:70
本文介绍了单选按钮的值求和功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试对单选按钮的值求和,每行div有两行,每行有2个单选按钮. 请参阅链接以获取更多详细信息: http://jsfiddle.net/HFuey/

I'm trying to sum values from radio buttons.Two rows of divs and 2 radio buttons per row. See link for more details: http://jsfiddle.net/HFuey/

JQ,我正在尝试使用:

JQ, I'm trying to use:

function recalculate() {
    var sum = 0;

    $("input[type=radio]:checked").each(function() {
        sum = parseFloat($(this).val();
    });

    $("#sum").html(sum.toFixed(2));
}

$("input[type=radio]").change(function() {
    recalculate();
});

有什么办法吗?我也可以使用href属性吗?

is there any way to do it ? can I use href attribute too ?

推荐答案

我想你想要

sum += parseFloat($(this).val());

以便编译(括号)并增加值(+=).

so that it compiles (parenthesis) and that the value is incremented (+=).

还请注意,您还必须在小提琴中导入jQuery,并且如果有一组,则实际上不会有一笔总和:一次只能选中一个单选按钮.

Note also that you must also import jQuery in the fiddle and that you won't really have a sum if you have one group : only one radio button may be checked at a time.

所以我想您要这个(链接到固定小提琴).

这篇关于单选按钮的值求和功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆