JavaScript帮助在按钮单击时运行基本功能

时间:2016-08-02 01:05:52

标签: javascript forms function button

按下“计算”按钮时,只是尝试运行一个简单的功能。当输入处于表单中时,函数根本不会运行,最终我希望能够在按下计算按钮时修改其他输入。任何帮助都可以!

function calculate() {
  alert("called");
}
<head>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
</head>
<form name="form">
  Price:
  <input name="priceCAD" value="0">
  <br>
  <br>Markup:
  <input name="percentage" value="0">
  <br>
  <br>Fiat:
  <input name="fiat" value="0">
  <br>
  <br>BTC:
  <input name="btc" value="0" maxlength="11">
  <br>
  <br>

  <input type="button" onClick="calculate()" name="calculate" value="Caculate">
  <input type="button" name="clear" value="Clear" onClick="form.fiat.value=0, form.btc.value=0, form.markup.value=0">
</form>

2 个答案:

答案 0 :(得分:1)

按钮HTML只需要它:

<input type="button" onClick="calculate()" name="calculate" value="Caculate" >

不要忘记在jquery lib中插入<head>

答案 1 :(得分:0)

&#13;
&#13;
function calculate() {
  alert("called");
}

document.getElementsByName("calculate")[0].addEventListener("click", calculate);
&#13;
<form name="form">
  Price:
  <input name="priceCAD" value="0">
  <br>
  <br>Markup:
  <input name="percentage" value="0">
  <br>
  <br>Fiat:
  <input name="fiat" value="0">
  <br>
  <br>BTC:
  <input name="btc" value="0" maxlength="11">
  <br>
  <br>

  <input type="button" name="calculate" value="Caculate">
  <input type="button" name="clear" value="Clear" onClick="form.fiat.value=0, form.btc.value=0, form.markup.value=0">
</form>
&#13;
&#13;
&#13;