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

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

问题描述

更新

如果您尝试此链接上的表格 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....

你的 crazy jQuery 代码来限制输入是荒谬的.. 你有名称、价值和你的数据价格属性...分割每个无线电设置项目对我来说似乎有点矫枉过正..

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天全站免登陆