简体   繁体   English

添加已检查的单选按钮总数

[英]Adding total of checked Radio Buttons

UPDATE UPDATE

If you try the form on this link http://jsfiddle.net/Matt_KP/BwmzQ/ the fiddle and select the top right £40 radio button then see the order total at the bottom it says £40. 如果您尝试在此链接上的表格http://jsfiddle.net/Matt_KP/BwmzQ/小提琴并选择右上方的40英镑单选按钮,然后在底部看到订单总额为40英镑。 Then if you select the £75 the order total changes to £75 but then if you go back and check the £40 again the order total is £75 + £40 when it should just be £40 for the radio button that is checked. 然后,如果您选择75英镑,订单总额将变为75英镑,但如果您再次返回并再次检查40英镑,则订单总额为75英镑+ 40英镑,而选中的单选按钮应为40英镑。

UPDATE END 更新结束

I have a section with Radio buttons where only certain radio buttons can be checked if others are selected. 我有一个带有单选按钮的部分,如果选择了其他按钮,则只能检查某些单选按钮。 So say if a user selected one Radio Button but then selected another the first Radio Button would become unselected as they cannot have both selected. 因此,如果用户选择了一个单选按钮但随后选择了另一个,则第一个单选按钮将被取消选中,因为它们不能同时选择这两个单选按钮。

Also I am using a custom attribute in the radio buttons called data-price which holds the value of each radio button that needs to be added toghther. 此外,我在名为data-price的单选按钮中使用自定义属性,该属性保存每个需要添加的单选按钮的值。

The problem is when a user selects a Radio Button the total shows fine but then if the user selects another radio button that can't have the previous one selected it adds the total onto the previous one where it should only add the Radio Buttons that are checked. 问题是当用户选择一个单选按钮时,总显示正常,但如果用户选择了另一个不能选择前一个单选按钮的单选按钮,它会将总数添加到前一个单选按钮上,它应该只添加单选按钮。检查。 It is kind of like caching the totals I think. 这有点像缓存我认为的总数。

This is what I am using to total the checked Radio Buttons: 这就是我使用的总计已检查的单选按钮:

<script type="text/javascript">
jQuery(document).ready(function($){
$('input:radio').change(function(){
var total = 0.0;
$('input:radio:checked').each(function(){
      total += parseFloat($(this).data('price'));
});
$('#total').val(total.toFixed(2));
});

})
</script>

I think the majority of your issues can be circumvented with some new HTML.... 我认为你的大多数问题都可以用一些新的HTML来规避....

Your crazy jQuery code to limit the input is ridiculous.. you have name, value, and your data-price attributes... splitting each radio set up by item seems a little overkill to me.. 疯狂的 jQuery代码限制输入是荒谬的..你有名字,价值和你的数据价格属性...按项目拆分每个收音机对我来说似乎有点矫枉过正..

Here is a limited example (as per our discussion in the chat). 这是一个有限的例子(根据我们在聊天中的讨论)。

http://jsfiddle.net/CZpnD/ <- here is the example you can work from.. http://jsfiddle.net/CZpnD/ < - 这是你可以使用的例子..

the main things to look at are how I used the same radio name for each "block" of options, and how I loop through all options when a single option is changed to get the new total (not the most efficient but it works). 要看的主要内容是我如何为每个“块”选项使用相同的无线电名称,以及如何在更改单个选项以获得新总数时循环所有选项(不是最有效但是有效)。

and for the love of pete use labels! 而对于皮特的爱用标签!

HTML is build to do this. 构建HTML是为了做到这一点。

<form name="myform">
    <input type="radio" name="foo" value="10" /> foo 
    <input type="radio" name="foo" value="30" /> bar 
</form>

If you give radio buttons the same name then only one can be selected. 如果给单选按钮指定相同的名称,则只能选择一个。

Further more when you get the radio element the .value property represents the value of the currently checked radio button 当你获得无线电元素时, .value属性表示当前检查的单选按钮的值

var myform = document.forms.myform;

var radio = myform.elements.foo;
var price = radio.value;

Note that radio is a RadioNodeList which is only returned by elements[name] 请注意, radioRadioNodeList ,仅由elements[name]返回

Example

However it turns out that browser support for RadioNodeList is appaling so you have to do it manually. 然而事实证明,对RadioNodeList的浏览器支持是适用的,所以你必须手动完成。 Or use the RadioNodeList polyfill 或者使用RadioNodeList polyfill

for (var i = 0, len = radio.length; i < len; i++) {
    var el = radio[i];
    if (el.checked) {
        var price = el.value;
        break;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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