.onchange不一致地改变输入

时间:2015-09-22 10:17:14

标签: javascript html input onchange

当我更改输入类型=" number" ID =" homevalue"在我改变另一个输入之前,它不会触发更改。所有其他输入立即触发更改,我看不出任何可以解释这一点的差异:

<body style="font-family: arial;">

  <form name="repaymentcalc" action="">
    <div align="center">
      </br>

      <p>
        Home Value £
        <input type="number" id="homevalue" value="250000" style="width: 75px">
      </p>

      <p>
        Loan Amount £
        <input type="number" id="loanamount" value="200000" style="width: 75px">
      </p>

      <p>
        Interest Rate
        <input type="number" id="interestrate" value="3.00" style="width: 50px">%
      </p>

      Term
      <input type="range" id="numberpayments" value="25" min="1" max="40" style="width: 100px">
      <div id="years" style="display:inline-block;">25 years

      </div>

      <div id="repayments">Monthly Repayment: £948.42</div>
      <p>
        <div id="ltv">Loan to Value: 80.0%</div>

    </div>
  </form>

  <script>
    window.onload = function() {
      document.repaymentcalc.loanamount.onchange = repayment;
      document.repaymentcalc.interestrate.onchange = repayment;
      document.repaymentcalc.numberpayments.onchange = repayment;

    }

    function repayment() {

      var x = parseInt(document.repaymentcalc.loanamount.value, 10);
      var y = parseInt(document.repaymentcalc.interestrate.value * 100, 10) / 120000;
      var z = parseInt(document.repaymentcalc.numberpayments.value, 10) * 12;
      var h = parseInt(document.repaymentcalc.homevalue.value, 10);

      var repayment = y * x * Math.pow((1 + y), z) / (Math.pow((1 + y), z) - 1);

      var loantovalue = x / h * 100;

      var year = z / 12;

      document.getElementById("repayments").innerHTML = 'Monthly Repayment: £' + repayment.toFixed(2);
      document.getElementById("ltv").innerHTML = 'Loan to Value: ' + loantovalue.toFixed(1) + '%';
      document.getElementById("years").innerHTML = year + ' years';

    }
  </script>

非常感谢任何帮助:)

感谢。

4 个答案:

答案 0 :(得分:0)

你试试这个,

import sys, os
django_path = os.path.join(os.path.dirname(__file__),"../../../")
sys.path.append(os.path.abspath(django_path))
os.environ['DJANGO_SETTINGS_MODULE'] = 'mysite.settings'

答案 1 :(得分:0)

或许可以使用oninput代替数字输入。只有在数字类型中使用本机号码选择器时才会触发onchange

请参阅以下代码片段,其中包含了两种类型的事件处理程序;

(function(){
  var inp = document.querySelector('input'),
      logChange = function() {
        console.log('there was a change');
      },
      logInput = function() {
        console.log('there was input'); 
      };
  
  inp.onchange = logChange;
  inp.oninput  = logInput;
}());
<!DOCTYPE html>
<head></head>
<body>
  <input type="number">
</body>

我认为在这种情况下oninput可能是更好的解决方案。 oninput会触发每个输入,而不会像输入onchange时那样输入失去焦点。有关此here的简短说明。

希望有所帮助!

答案 2 :(得分:0)

试试这个:

  如果通过鼠标单击更改值,则还需要

onmouseup 事件。用户可以通过单击箭头按钮或键入输入来更改值,在下面的示例中,两个案例都会被处理。

&#13;
&#13;
@style/Theme.AppCompat.NoActionBar
&#13;
 window.onload = function() {
   document.repaymentcalc.loanamount.onkeyup = repayment;
   document.repaymentcalc.interestrate.onkeyup = repayment;
   document.repaymentcalc.numberpayments.onkeyup = repayment;

   document.repaymentcalc.loanamount.onmouseup = repayment;
   document.repaymentcalc.interestrate.onmouseup = repayment;
   document.repaymentcalc.numberpayments.onmouseup = repayment;

 };

 function repayment() {
   console.log("Here");
   var x = parseInt(document.repaymentcalc.loanamount.value, 10);
   var y = parseInt(document.repaymentcalc.interestrate.value * 100, 10) / 120000;
   var z = parseInt(document.repaymentcalc.numberpayments.value, 10) * 12;
   var h = parseInt(document.repaymentcalc.homevalue.value, 10);

   var repayment = y * x * Math.pow((1 + y), z) / (Math.pow((1 + y), z) - 1);

   var loantovalue = x / h * 100;

   var year = z / 12;

   document.getElementById("repayments").innerHTML = 'Monthly Repayment: £' + repayment.toFixed(2);
   document.getElementById("ltv").innerHTML = 'Loan to Value: ' + loantovalue.toFixed(1) + '%';
   document.getElementById("years").innerHTML = year + ' years';

 }
&#13;
&#13;
&#13;

答案 3 :(得分:0)

Demo

它没有触发,因为您没有为onchange定义homevalue事件。你可以添加如下。

window.onload = function() {
      document.repaymentcalc.homevalue.onchange = repayment;
      document.repaymentcalc.loanamount.onchange = repayment;
      document.repaymentcalc.interestrate.onchange = repayment;
      document.repaymentcalc.numberpayments.onchange = repayment;

    }

此外,homevalue具有很大的价值,它实际上会因精确而改变但不会影响。我已经在演示中更改了homevalue,您可以尝试完全正常的演示。