计算单选按钮的总和,显示总和,并传递给外部 Beanstream 服务器

Calculating the sum of radio buttons, displaying the sum, and passing to external Beanstream server

本文关键字:外部 服务器 Beanstream 显示 单选按钮 计算      更新时间:2023-09-26

我是一个新手Web开发人员,我需要计算一些具有不同值的单选按钮的总和,然后将该结果传递给外部支付服务器。

这是其中一个单选按钮的样子:

<div class='container'>
<label for='p_board_rental' ><strong>Paddleboard Rental</strong> (limited amount available*: </label><br/>
<input type='radio' name='rental' value='0'/><label class="radio"> No</label><br/>
<input type='radio' name='rental' value='20'/><label class="radio"> Yes</label><br/>

我需要计算最终结果然后显示,但我尝试了很多方法,但我无法让它正常工作!我能得到的最好的是应该出现总数的空框。

如果你使用的是jQuery,你可以做如下的事情:

function tallyValues(){
    var tally = 0;
    $('input:radio').each(function(){
        if($(this).is(':checked')){
         tally += parseInt($(this).val());
        }
    });
}

Tally 将保存所选值的总和

这应该让你接近你想去的地方。 只需遍历所有单选按钮,如果选中,则将其推入数组(如果数组中尚不存在)。 然后在最后计算总数并在文本框中设置。

可能有一些jquery函数可以使它更优雅一些。

var array = new Array();
$(":input[type='radio']", "#containerOfChoice").each(function () {
    if ($(this).is(':checked')) {
        var value = $(this).attr("value");
        if ($.inArray(value, array) == -1) {
            array.push(value);
        }
    }
})
var total = 0;
for (var i = 0; i < array.length; i++) {
    total += parseInt(array[i]);
}
$("#textBoxOfChoice").val(total);