无法获取Javascript变量的表单输入

时间:2015-12-31 13:04:57

标签: javascript html

我是Javascript的新手...而且我无法从输入到js变量,我之前提到的问题仍然是我无法获取信息。我的代码如下: -

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>JS </title>
<body>

    <form>
    <input type="text" id="amount" placeholder="Amount">
    <button type="button" onclick="calc()">Submit</button>
    </form>

    <h1 id="TotalAmout"></h1>
    <script>
        function calc() {
    var amt = document.getElementById("amount").value;

    document.getElementById("TotalAmount").innerHTML = amt;
    }
    </script>
</body>
</html>

5 个答案:

答案 0 :(得分:0)

您的H1身份证上有拼写错误。它应该与您的javascript中的TotalAmount匹配。

答案 1 :(得分:0)

  1. H1 TotalAmout中的错字应为TotalAmount

  2. 应在调用之前定义Calc。 Fiddle

    <head>
      function calc() {
        var amt = document.getElementById("amount").value;
        document.getElementById("TotalAmount").innerHTML = amt;
      }
    </head> 
    <body>
      <form>
        <input type="text" id="amount" placeholder="Amount">
        <button type="button" onclick="javascript:calc()">Submit</button>
      </form>
     <h1 id="TotalAmount"></h1>
    </body>
    

答案 2 :(得分:0)

<form>
<input type="text" id="amount" placeholder="Amount">
<button type="button" onclick="calc();return false">Submit</button>
</form>

<h1 id="TotalAmout"></h1>
<script>
    function calc() {
var amt = $("#amount").val();

$("TotalAmount").html(amt);
}
</script>

答案 3 :(得分:0)

两件事:

  • 错误&#34; TotalAmout&#34;和&#34; TotalAmount&#34;
  • 函数需要在调用之前定义

见这里:https://jsfiddle.net/zLguL6fu/

<form>
  <input type="text" id="amount" placeholder="Amount">
  <button type="button" onclick="calc(); return false;">Submit</button>
</form>
<h1 id="TotalAmount"></h1>

function calc() {
   var amt = document.getElementById("amount").value;

   document.getElementById("TotalAmount").innerHTML = amt;
 }

答案 4 :(得分:0)

请使用此代码。

    <!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>JS </title>
    <script src="//code.jquery.com/jquery-1.11.3.min.js"></script>
</head>
<body>

    <form>
    <input type="text" id="amount" placeholder="Amount">
    <button type="button" onclick="calc()">Submit</button>
    </form>

    <h1 id="TotalAmout"></h1>
    <script>
        function calc() {
            var amt = $("#amount").val();
            $("#TotalAmout").html(amt);
        }
    </script>
</body>
</html>

你还需要在头部添加jquery js