简体   繁体   English

如果输出小于零,则隐藏输出

[英]Hide Output if output is less than zero

I finished a savings calculator but I realized that I want to display a different div with alternative options if the savings output is a negative number. 我完成了一个储蓄计算器,但是我意识到如果储蓄输出为负数,我想显示一个带有其他选项的div。 I have tried a few different things and nothings has worked. 我尝试了几种不同的方法,但没有任何效果。 I am newer to jQuery and I am having a bit of trouble. 我刚接触jQuery,但遇到了一些麻烦。 Any help is appreciated. 任何帮助表示赞赏。 Below is my code: 下面是我的代码:

    function toCurrency(str) {
    let ret = parseFloat(str[0] === '$' ? str.substr(1) : str).toFixed(2).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
    if (!isNaN(ret))
    return str;
    return ret;
}

function calculate(){
 var savingsVal = (jQuery('#savingsVal').val());
 var totalSavings = 0;
 var regType = (jQuery('#reg-type').val());



 if (filterValues(savingsVal)) {
 totalSavings = savingsVal * 0.15 * 3 - regType * 3;
 }
 jQuery('#totalSavings').val('$' + toCurrency(totalSavings));
 }

if (totalSavings < 0) {
    jQuery("#negative-numbers").show();
}

else{
  jQuery("#negative-numbers").hide();
}




 function filterValues(eVal){
 return true;

 }

 jQuery('.results-area').hide();

 jQuery('#calculator').submit(function(e){
 e.preventDefault();
 calculate();
 jQuery('.results-area').show("slow");
 });

You have some logical errors in your code that are preventing it from working as expected. 您的代码中存在一些逻辑错误,这些错误阻止了代码正常工作。 The number 1 mistake, is common, serious and preventable: 数字1错误是常见,严重且可预防的:

Forgetting to declare a variable. 忘记声明变量。

In your code, totalSavings is used in two scopes but one of them neglects to declare it. 在您的代码中, totalSavings在两个范围中使用,但是其中一个忽略声明它。

To prevent this error, and to provide enhanced performance and security, always start every script with "use strict"; 为了防止该错误并提供增强的性能和安全性,请始终以"use strict";启动每个脚本"use strict"; to enable ES5 strict mode. 启用ES5严格模式。

Under strict mode, undeclared variables throw an error. 在严格模式下,未声明的变量将引发错误。

Furthermore, the toggling error itself was due to placing your if, if (totalSavings < 0) statement in global scope instead of within the calculate function or the event handler itself. 此外,切换错误本身是由于将if, if (totalSavings < 0)语句放置在全局范围内,而不是在calculate函数或事件处理程序本身内引起的。

Coupled with failing to declare the totalSavings variable, making the condition initially false as well, undefined is neither greater than nor less than 0 . 再加上未能声明totalSavings变量,使条件最初也为false, undefined既不大于也不小于0

I have corrected these errors in the code below. 我已在下面的代码中更正了这些错误。

 'use strict'; (function() { var totalSavings = 0; function toCurrency(value) { let result = parseFloat( value[0] === '$' ? value.substr(1) : value ).toFixed(2).toString().replace(/\\B(?=(\\d{3})+(?!\\d))/g, ','); return isNaN(result) ? value : result; } function calculate() { var savingsVal = jQuery('#savingsVal').val(); var totalSavings = 0; var regType = jQuery('#reg-type').val(); if (filterValues(savingsVal)) { totalSavings = savingsVal * 0.15 * 3 - regType * 3; } return totalSavings; } function filterValues(eVal) { return true; } jQuery('#calculator').click(function(e) { e.preventDefault(); totalSavings = calculate(); jQuery('.results-area').show("slow"); jQuery('#totalSavings').text('$' + toCurrency(totalSavings)); if (totalSavings < 0) { jQuery('#negative-numbers') .show() .find('#negative-value') .text(totalSavings) .show(); jQuery('.results-area').hide(); } else { jQuery('#negative-numbers').hide(); } }); }()); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> <label>Reg Type</label> <input type="text" id="reg-type"> <br> <label>Savings</label> <input type="text" id="savingsVal"> <br> <button id="calculator">Submit</button> <div></div> <div id="negative-numbers">Negative Numbers <div id="negative-value"></div> </div> <div class="results-area">Results <div id="totalSavings">Total </div> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM