总共添加选中的单选按钮 [英] Adding total of checked Radio Buttons

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

问题描述

更新

如果您尝试通过此链接上的表单 http://jsfiddle.net/Matt_KP/BwmzQ/拨弄并选择右上角的£40单选按钮,然后在底部看到订单总额为£40的订单.然后,如果您选择75英镑,则订单总额将更改为75英镑,但是如果您再次返回并检查40英镑,则订单总额为75英镑+ 40英镑,而选中的单选按钮的订单总额应为40英镑.

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. 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.

更新结束

我有一个带有单选按钮的部分,如果选择了其他单选按钮,则只能检查某些单选按钮.假设如果用户选择了一个单选按钮,然后又选择了另一个,则第一个单选按钮将无法选择,因为他们不能同时选择两个.

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.

我还在名为data-price的单选按钮中使用自定义属性,该属性保存需要添加到每个单选按钮的值.

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.

问题是,当用户选择一个单选按钮时,总计显示正常,但是如果用户选择另一个不能选择上一个按钮的单选按钮,则会将总数添加到前一个按钮上,而该按钮只能添加单选按钮选中的按钮.我觉得这有点像在缓存总数.

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>

推荐答案

我认为您的大多数问题都可以通过一些新的HTML来规避....

I think the majority of your issues can be circumvented with some new HTML....

您的 疯狂 限制输入的jQuery代码是荒谬的..您具有名称,值和数据价格属性...将每个无线电设置拆分为该项目对我来说似乎有点矫kill过正.

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..

这是一个有限的示例(根据我们在聊天中的讨论).

Here is a limited example (as per our discussion in the chat).

http://jsfiddle.net/CZpnD/<-这是您可以使用的示例.

http://jsfiddle.net/CZpnD/ <- here is the example you can work from..

要查看的主要内容是如何对每个选项的块"使用相同的单选名称,以及当更改单个选项以获取新的总数时如何遍历所有选项(不是最有效的,但它作品).

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!

这篇关于总共添加选中的单选按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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