我修改后,jquery计算脚本不工作 [英] Jquery calculation script not working after I modified it

查看:83
本文介绍了我修改后,jquery计算脚本不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是一个Jquery插件,用于汇总在您选择复选框或单选按钮时输入到名为 data-cost 的数据属性中的值。我修改了脚本,以便可以将值输入到文本输入中

I am using a Jquery plugin that sums up values that are entered into data attributes named data-cost when you select the checkboxes or radio buttons. I modified the script so you could enter values into a text input

例如,脚本在

So for example, the script is triggered when a user enters the value of the text input with as seen below.

<input type="text" class="balance" id="account_balance1" name="cardbalance" value=""/>

它改变了这个输入中的数据属性

It changes the data attribute in this input

<input type="checkbox" checked="checked" data-cost="100" debt="" value="" name="f_2[]"/>

但是在添加了9个复选框输入实例文本输入,它不会将蓝色总计框中的10个实例总结在右边,如此小提琴中所示。当你第一次加载小提琴时,你会注意到它总计1000美元,直到做出更改。之后,它只是将值更改为任何你输入的值,并在你输入的值的右边添加一个零。

But after adding 9 additional instances of the checkbox input and the text input, it does not sum up the 10 instances in the blue total box to the right as seen in this fiddle. When you first load the fiddle you will notice it sums $1000 until the changes are made. After that it just changes the value to whatever you type and it adds a zero to the right of the value you enter.

在这里你可以看到一个有效的例子。 http://preview.benmartinstudios.com.au/jquery-price -calculator-pro / quantities.html

Here you can see a working example with my modifications. http://preview.benmartinstudios.com.au/jquery-price-calculator-pro/quantities.html

这是小提琴 http://jsfiddle.net/mikeef74/8jdfJ/11/

推荐答案

当输入的值发生变化时,我想你只是想更新关联复选框上的 data - 属性。您正在更新所有复选框的值。

When the value of an input is changed, I think you just want the data- attributes on the associated checkbox updated. You are updating the values on all the checkboxes.

像这样:

Like this:

$('.balance').on('keyup blur paste', function() {
    var $self = $(this),
        $checkbox = $self.closest('li').find('input:checkbox');
    setTimeout(function() {
        var str = $self.val();
        $checkbox.data('cost',str.replace(/^\$/, ''));
        $checkbox.data('debt',str.replace(/^\$/, ''));
        calculateTotalFor();
    }, 0)
});

jsfiddle

这篇关于我修改后,jquery计算脚本不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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