Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/meteor/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 当零值或负值是变量时,如何创建特定的错误窗口?_Javascript - Fatal编程技术网

Javascript 当零值或负值是变量时,如何创建特定的错误窗口?

Javascript 当零值或负值是变量时,如何创建特定的错误窗口?,javascript,Javascript,我在网上学习制作贷款计算器的教程。计算器工作正常,但当你在其中一个变量中输入零或负数时,我试图设置一个错误窗口(你不能有负/零贷款金额、利率或年数)。我希望能够创建一个特定的错误窗口,在贷款金额中输入零或负数时,可以说“不能将零或负数作为贷款金额的值”。对于其他变量也一样,如果有意义的话,可以在其中输入值。 当你打开计算器时,我能弹出一个错误窗口,上面写着“你不能输入任何负数或零”,当你输入零或负数时,这个窗口也会弹出。我试图调用每个变量的const并弹出窗口,但没有成功 documen

我在网上学习制作贷款计算器的教程。计算器工作正常,但当你在其中一个变量中输入零或负数时,我试图设置一个错误窗口(你不能有负/零贷款金额、利率或年数)。我希望能够创建一个特定的错误窗口,在贷款金额中输入零或负数时,可以说“不能将零或负数作为贷款金额的值”。对于其他变量也一样,如果有意义的话,可以在其中输入值。 当你打开计算器时,我能弹出一个错误窗口,上面写着“你不能输入任何负数或零”,当你输入零或负数时,这个窗口也会弹出。我试图调用每个变量的const并弹出窗口,但没有成功

    document.getElementById("loan-form").addEventListener("submit", computeResults);

function computeResults(e) {
  // UI

  const UIamount = document.getElementById("amount").value;
  const UIinterest = document.getElementById("interest").value;
  const UIyears = document.getElementById("years").value;
  hola(UIamount);
  hola(UIinterest);
  hola(UIyears);

  // Calculate

  const principal = parseFloat(UIamount);
  const CalculateInterest = parseFloat(UIinterest) / 100 / 12;
  const calculatedPayments = parseFloat(UIyears) * 12;

  //Compute monthly Payment

  const x = Math.pow(1 + CalculateInterest, calculatedPayments);
  const monthly = (principal * x * CalculateInterest) / (x - 1);
  const monthlyPayment = monthly.toFixed(2);

  //Compute Interest

  const totalInterest = (monthly *calculatedPayments - principal).toFixed(2);

  //Compute Total Payment

  const totalPayment = (monthly * calculatedPayments).toFixed(2);

  //Show results

  document.getElementById("monthlyPayment").innerHTML = "$" + monthlyPayment;

  document.getElementById("totalInterest").innerHTML = "%" + totalInterest;

  document.getElementById("totalPayment").innerHTML = "$" + totalPayment;

  e.preventDefault();
}
function hola(x) {
  console.log(x);
  if(x == 0) {
      document.getElementById("cont1").style.display="none";

  }

  if(x == 1) {
      document.getElementById("cont1").style.display="none";

  }

  if(x<=0)  {
      document.getElementById("cont1").style.display="block";

  }
}
document.getElementById(“贷款表格”).addEventListener(“提交”,计算机结果);
功能计算机结果(e){
//用户界面
const UIamount=document.getElementById(“金额”).value;
const UIinterest=document.getElementById(“interest”).value;
const UIyears=document.getElementById(“年”).value;
hola(UIamount);
霍拉(UIC);
霍拉(10年);
//算计
const principal=parseFloat(UIamount);
const CalculateInterest=parseFloat(UIinterest)/100/12;
const calculatedPayments=parseFloat(UIyears)*12;
//计算每月付款
常数x=数学功率(1+计算利息,计算付款);
const monthly=(本金*x*计算利息)/(x-1);
const monthlyPayment=每月固定(2);
//计算利息
const totalInterest=(每月*计算的付款-本金)。toFixed(2);
//计算总付款额
施工总付款=(每月*计算付款)。固定(2);
//显示结果
document.getElementById(“monthlyPayment”).innerHTML=“$”+monthlyPayment;
document.getElementById(“totalInterest”).innerHTML=“%”+totalInterest;
document.getElementById(“totalPayment”).innerHTML=“$”+totalPayment;
e、 预防默认值();
}
函数hola(x){
控制台日志(x);
如果(x==0){
document.getElementById(“cont1”).style.display=“无”;
}
如果(x==1){
document.getElementById(“cont1”).style.display=“无”;
}

如果(x代码中的一个问题是,如果一个值无效,但其下的其他值有效,则错误将被隐藏

使用if语句代替函数:

const UIamount = document.getElementById("amount").value;
const UIinterest = document.getElementById("interest").value;
const UIyears = document.getElementById("years").value;
let err_box = document.getElementById("cont1");

err_box.style.display="none";//hide the popup first

if(UIamount <= 0 || UIinterest <= 0 || UIyears <= 0){
   err_box.style.display="block";
}
const UIamount=document.getElementById(“amount”).value;
const UIinterest=document.getElementById(“interest”).value;
const UIyears=document.getElementById(“年”).value;
设err_box=document.getElementById(“cont1”);
err_box.style.display=“none”//首先隐藏弹出窗口

如果(UIamount代码中的一个问题是,如果一个值无效,但其下的其他值有效,则错误将被隐藏

使用if语句代替函数:

const UIamount = document.getElementById("amount").value;
const UIinterest = document.getElementById("interest").value;
const UIyears = document.getElementById("years").value;
let err_box = document.getElementById("cont1");

err_box.style.display="none";//hide the popup first

if(UIamount <= 0 || UIinterest <= 0 || UIyears <= 0){
   err_box.style.display="block";
}
const UIamount=document.getElementById(“amount”).value;
const UIinterest=document.getElementById(“interest”).value;
const UIyears=document.getElementById(“年”).value;
设err_box=document.getElementById(“cont1”);
err_box.style.display=“none”//首先隐藏弹出窗口

如果(ui)当您尝试将错误放入其中一个变量时,不会出现错误。当用户离开输入数据的字段时,会出现错误。您需要向该字段添加
change
事件处理程序,并在其中进行验证。当您尝试将错误放入其中一个变量时,不会出现错误变量。当用户离开输入数据的字段时,就会出现该变量。您需要向该字段添加
change
事件处理程序,并在那里进行验证。