Passing HTML input value as a JavaScript Function Parameter

后端 未结 7 1722
别跟我提以往
别跟我提以往 2020-12-13 15:52

I am new to JavaScript, and I\'m trying to figure out how to pass user-inputted values as a parameter to a JavaScript function. Here is my code:

         


        
相关标签:
7条回答
  • 2020-12-13 16:09

    You can get the values with use of ID. But ID should be Unique.

    <body>
    <h1>Adding 'a' and 'b'</h1>
    <form>
      a: <input type="number" name="a" id="a"><br>
      b: <input type="number" name="b" id="b"><br>
      <button onclick="add()">Add</button>
    </form>
    <script>
      function add() {
        a = $('#a').val();
        b = $('#b').val();
        var sum = a + b;
        alert(sum);
      }
    </script>
    </body>
    
    0 讨论(0)
  • 2020-12-13 16:11
       <form action="" onsubmit="additon()" name="form1" id="form1">
          a: <input type="number" name="a" id="a"><br>
          b: <input type="number" name="b" id="b"><br>
          <input type="submit" value="Submit" name="submit">
       </form>
      <script>
          function additon() 
          {
               var a = document.getElementById('a').value;
               var b = document.getElementById('b').value;
               var sum = parseInt(a) + parseInt(b);
               return sum;
          }
      </script>
    
    0 讨论(0)
  • 2020-12-13 16:12

    Firstly an elements ID should always be unique. If your element IDs aren't unique then you would always get conflicting results. Imagine in your case using two different elements with the same ID.

    <form>
      a: <input type="number" name="a" id="a"><br>
      b: <input type="number" name="b" id="b"><br>
      <button onclick="add()">Add</button>
    </form>
    
    <script>
      function add() {
        var a = document.getElementById('a').value;
        var b = document.getElementById('b').value;
    
        var sum = parseInt(a) + parseInt(b);
        alert(sum);
      }
    </script>
    
    0 讨论(0)
  • 2020-12-13 16:16

    do you use jquery? if then:

    $('#xx').val();
    

    or use original javascript(DOM)

    document.getElementById('xx').value
    

    or

    xxxform.xx.value;
    

    if you want to learn more, w3chool can help you a lot.

    0 讨论(0)
  • 2020-12-13 16:16

    Use this it will work,

    <body>
    <h1>Adding 'a' and 'b'</h1>
    <form>
      a: <input type="number" name="a" id="a"><br>
      b: <input type="number" name="b" id="a"><br>
      <button onclick="add()">Add</button>
    </form>
    <script>
      function add() {
        var m = document.getElementById("a").value;
        var n = document.getElementById("b").value;
        var sum = m + n;
        alert(sum);
      }
    </script>
    </body>
    
    0 讨论(0)
  • 2020-12-13 16:23

    1 IDs are meant to be unique

    2 You dont need to pass any argument as you can call them in your javascript

    <form>
      a: <input type="number" name="a" id="a"><br>
      b: <input type="number" name="b" id="b"><br>
      <button onclick="add()">Add</button>
    </form>
    <script>
      function add() {
        var a = document.getElementById('a').value;
        var b = document.getElementById('b').value;
        var sum = a + b;
        alert(sum);
      }
    </script>
    
    0 讨论(0)
提交回复
热议问题