简单的数学减法(jquery)

时间:2013-12-07 17:38:39

标签: javascript jquery math

我需要显示简单数学减法的html。

treshold - count = currentCount

以下是我当前的脚本 - 它可以正常隐藏/显示DIV。但我需要更改代码,以便在html中显示currentCount

var threshold = 100; // Number to trigger on

var widgets_to_hide = ["#promotion"]; // CSS selectors of widgets to hide when triggered

var widgets_to_show = ["#text"]; // CSS selectors of widgets to show when triggered



$(function(){
$('.ss_entry_count').on('changed.content', function(event){
var count = parseInt($('.ss_entry_count_value', this).text());


if(count - threshold){


$.each(widgets_to_hide, function(i){
$(widgets_to_hide[i]).hide();
});


$.each(widgets_to_show, function(i){
$(widgets_to_show[i]).show();
});


}
});
});


<span id="result"></span>

1 个答案:

答案 0 :(得分:1)

您需要将treshold - count分配给currentCount,然后设置#result的文字。另外,不要忘记将基数参数添加到parseInt

$(function(){
    $('.ss_entry_count').on('changed.content', function(event){
        var count = parseInt($('.ss_entry_count_value', this).text(), 10);
        var currentCount = treshold - count;
        if(currentCount){
            $.each(widgets_to_hide, function(i, widget){
                $(widget).hide();
            });
            $.each(widgets_to_show, function(i, widget){
                $(widget).show();
            });
        }

        $("#result").text(currentCount);
    });
});

您可以将此更简化为:

// use a single selector to specify which widgets to show / hide
var widgets_to_hide = "#promotion";
var widgets_to_show = "#text";

$(function(){
    $('.ss_entry_count').on('changed.content', function(event){
        var count = parseInt($('.ss_entry_count_value', this).text(), 10);
        var currentCount = treshold - count;
        if(currentCount){
            $(widgets_to_hide).hide();
            $(widgets_to_show).show();
        }

        $("#result").text(currentCount);
    });
});