添加选中的单选按钮总数

2023-10-21前端开发问题
2

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

问题描述

更新

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

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

The End

相关推荐

js删除数组中指定元素的5种方法
在JavaScript中,我们有多种方法可以删除数组中的指定元素。以下给出了5种常见的方法并提供了相应的代码示例: 1.使用splice()方法: let array = [0, 1, 2, 3, 4, 5];let index = array.indexOf(2);if (index -1) { array.splice(index, 1);}// array = [0,...
2024-11-22 前端开发问题
182

layui中表单会自动刷新的问题
layui中表单会自动刷新的问题,因为用到layui的表单,遇到了刷新的问题所以记录一下: script layui.use(['jquery','form','layer'], function(){ var $ = layui.jquery, layer=layui.layer, form = layui.form; form.on('submit(tijiao)', function(data){ a...
2024-10-23 前端开发问题
262

JavaScript小数运算出现多位的解决办法
在开发JS过程中,会经常遇到两个小数相运算的情况,但是运算结果却与预期不同,调试一下发现计算结果竟然有那么长一串尾巴。如下图所示: 产生原因: JavaScript对小数运算会先转成二进制,运算完毕再转回十进制,过程中会有丢失,不过不是所有的小数间运算会...
2024-10-18 前端开发问题
301

jQuery怎么动态向页面添加代码?
append() 方法在被选元素的结尾(仍然在内部)插入指定内容。 语法: $(selector).append( content ) var creatPrintList = function(data){ var innerHtml = ""; for(var i =0;i data.length;i++){ innerHtml +="li class='contentLi'"; innerHtml +="a href...
2024-10-18 前端开发问题
125

JavaScript(js)文件字符串中丢失"\"斜线的解决方法
问题描述: 在javascript中引用js代码,然后导致反斜杠丢失,发现字符串中的所有\信息丢失。比如在js中引用input type=text onkeyup=value=value.replace(/[^\d]/g,) ,结果导致正则表达式中的\丢失。 问题原因: 该字符串含有\,javascript对字符串进行了转...
2024-10-17 前端开发问题
437

layui中table列表 增加属性 edit="date",不生效怎么办?
如果你想在 layui 的 table 列表中增加 edit=date 属性但不生效,可能是以下问题导致的: 1. 缺少日期组件的初始化 如果想在表格中使用日期组件,需要在页面中引入 layui 的日期组件,并初始化: script type="text/javascript" src="/layui/layui.js"/scrip...
2024-06-11 前端开发问题
455