当按钮计算点击

时间:2015-05-21 01:51:16

标签: javascript html

我无法在文本字段中显示我的贷款计算器的计算,这是我的代码..我希望您可以帮助我。



var select = document.getElementById('loantype');
var input = document.getElementById('interest');
select.onchange = function() {
  input.value = select.value;
}

function calculate() {
  // Get the user's input from the form. Assume it is all valid.
  // Convert interest from a percentage to a decimal, and convert from
  // an annual rate to a monthly rate. Convert amountOfPayement period in years
  // to the number of monthly payments.
  var amountBorrowed = document.form.amountBorrowed.value;
  var interest = document.form.interest.value / 100 / 12;
  var payments = document.form.durationOfPayment.value * 12;

  // Now compute the monthly amountOfPayement figure, using esoteric math.
  var x = Math.pow(1 + interest, payments);
  var monthly = (amountBorrowed * x * interest) / (x - 1);

  // Check that the result is a finite number. If so, display the results
  if (!isNaN(monthly) &&
    (monthly != Number.POSITIVE_INFINITY) &&
    (monthly != Number.NEGATIVE_INFINITY)) {

    document.form.amountOfPayement.value = round(monthly);
    document.form.total.value = round(monthly * payments);
    document.form.totalinterest.value =
      round((monthly * payments) - amountBorrowed);
  }
  // Otherwise, the user's input was probably invalid, so don't
  // display anything.
  else {
    document.form.amountOfPayement.value = "";
    document.form.total.value = "";
    document.form.totalinterest.value = "";
  }
}

// This simple method rounds a number to two decimal places.
function round(x) {
  return Math.round(x * 100) / 100;
}

<form name="form" action="action.php" method="post" onsubmit="return validate()">

  <div class="wrapper">
    <span>Select Loan Type</span>
    <br>
    <select type="text" id="loantype" name="loantype">
      <option value="walanglaman" selected="selected" disabled>LOAN TYPE:</option>
      <option value="6">BUSINESS LOAN</option>
      <option value="3">PERSONAL LOAN</option>
      <option value="4">SEAMAN LOAN</option>
      <option value="4">COLLATERALIZED LOAN</option>
      <option value="3">REGULAR LOAN LOAN</option>
      <option value="5">CHECK REDISCOUNTING</option>
      <option value="4">LOAN AGAINST DEPOSIT</option>
    </select>&nbsp;
    <label id="errorThree"></label>


    <br>
    <br><span>Mobile Number(Required)</span>
    <br>
    <input type="integer" name="contactNo" id="contactNo" maxlength="11" />&nbsp;
    <label id="errorEight"></label>&nbsp;
    <label id="errorAlphaFour"></label>&nbsp;
    <label id="errorMinThree"></label>&nbsp;
    <label id="invalidFormat"></label>
    <br>

    <br><span>Email Address(Optional)</span>
    <br>
    <input type="text" name="email" id="email" placeholder="Email Address" onfocus="this.placeholder=''" onblur="this.placeholder='Email Address'" />&nbsp;
    <label id="errorNine"></label>&nbsp;
    <label id="errorAlphaFive"></label>
    <br>

    <br>
    <span>Reason of Loan</span>
    <br>
    <select type="text" id="reasonOfLoan" name="reasonOfLoan">
      <option value="empty4" selected="selected" disabled>Select One</option>
      <option value="For Emergency">For Emergency</option>
      <option value="For Business">For Business</option>
    </select>
    <label id="errorTwelve"></label>
    <br>
  </div>

  <div class="contentsTwo">
    <br><span>Amount Borrowed(Php)</span>
    <br>
    <input type="integer" name="amountBorrowed" id="amountBorrowed" maxlength="7" placeholder="Amount Borrowed" onfocus="this.placeholder=''" onblur="this.placeholder='Amount Borrowed'" />
    <label id="errorThirteen"></label>&nbsp;
    <label id="errorAlphaSeven"></label>&nbsp;
    <label id="errorMinSix"></label>&nbsp;
    <label id="errorInvalidFormatTwo"></label>
    <br>
    <br><span>Interest (%)</span>
    <br>
    <input type="integer" name="interest" id="interest" maxlength="6" disabled="disabled" onchange="checkPrice()" />
    <br>
    <span>Duration of Payment</span>
    <br>
    <select type="text" id="durationOfPayment" name="durationOfPayment" onchange="calculate();">
      <option value="empty5" selected="selected" disabled>Select One</option>
      <option value="3">Three Months</option>
      <option value="6">Six Months</option>
      <option value="12">One Year</option>
      <option value="18">One 1/2 Year</option>
    </select>
    <label id="errorFifteen"></label>
    <br>
    <br>

    <input id="calculate" type="button" name="calculate" value="Calculate Loan" onclick="calculate();" />
    <br>
    <br>
    <br><span>Amount Of Payment Per Month (Php)</span>
    <br>
    <input type="integer" name="amountOfPayment" id="amountOfPayment" maxlength="6" onchange="calculate();" disabled="disabled" />
    <label id="errorFourteen"></label>&nbsp;
    <label id="errorAlphaEight"></label>&nbsp;
    <label id="errorMinSeven"></label>&nbsp;
    <label id="errorInvalidFormatThree"></label>
    <br>
    <br><span>Total Interest</span>
    <br>
    <input type="integer" name="totalinterest" id="totalinterest" maxlength="6" disabled="disabled" onchange="checkPrice()" />
    <br>
    <br><span>Total</span>
    <br>
    <input type="integer" name="total" id="total" maxlength="6" disabled="disabled" />
    <br>
    <br>

    <label id="errorOther"></label>
    <br>
  </div>
  <div class="btn">
    <br>
    <br>
    <br>
    <br>
    <br>
    <br>
    <br>
    <br>
    <br>
    <br>
    <br>
    <input id="submit" type="submit" name="submit" value="Submit" />
    <br>
    <br>

  </div>

</form>
&#13;
&#13;
&#13;

当我点击计算时,什么也没发生,我真的会帮助你的帮助人员

2 个答案:

答案 0 :(得分:2)

问题在于:

<input id="calculate" type="button" name="calculate" value="Calculate Loan" onclick="calculate();" />

所有id都成为全局变量,因此名称calculate引用此输入元素而不是函数。更改此id或更改函数名称。

你也有拼写错误。在Javascript中,您引用了document.form.amountOfPayement,但在HTML中它是name="amountOfPayment"。这些需要匹配。

这两个问题都导致Javascript控制台出错。第一个抱怨calculate is not a function,第二个抱怨尝试访问value的{​​{1}}属性时出错。我不确定你为什么在浏览器中没有看到这些错误。

undefined
var select = document.getElementById('loantype');
var input = document.getElementById('interest');
select.onchange = function() {
  input.value = select.value;
}

function calculate_loan() {
  // Get the user's input from the form. Assume it is all valid.
  // Convert interest from a percentage to a decimal, and convert from
  // an annual rate to a monthly rate. Convert amountOfPayement period in years
  // to the number of monthly payments.
  var amountBorrowed = document.form.amountBorrowed.value;
  var interest = document.form.interest.value / 100 / 12;
  var payments = document.form.durationOfPayment.value * 12;

  // Now compute the monthly amountOfPayement figure, using esoteric math.
  var x = Math.pow(1 + interest, payments);
  var monthly = (amountBorrowed * x * interest) / (x - 1);

  // Check that the result is a finite number. If so, display the results
  if (!isNaN(monthly) &&
    (monthly != Number.POSITIVE_INFINITY) &&
    (monthly != Number.NEGATIVE_INFINITY)) {

    document.form.amountOfPayement.value = round(monthly);
    document.form.total.value = round(monthly * payments);
    document.form.totalinterest.value =
      round((monthly * payments) - amountBorrowed);
  }
  // Otherwise, the user's input was probably invalid, so don't
  // display anything.
  else {
    document.form.amountOfPayement.value = "";
    document.form.total.value = "";
    document.form.totalinterest.value = "";
  }
}

// This simple method rounds a number to two decimal places.
function round(x) {
  return Math.round(x * 100) / 100;
}

答案 1 :(得分:-3)

你需要把这个INTO放在你的表格中:

<script>
var select = document.getElementById('loantype');
var input = document.getElementById('interest');
select.onchange = function() {
  input.value = select.value;
}
</script>

之后的元素