如何将变量设置为表单输入的输出?

How to set a variable to the output of a form's input?

假设我有以下代码,我希望 N1 和 N2 分别等于 numberInput1 和 numberInput2 的输入。我该怎么做?

"use strict";
// Alternative formula: MN = (N1 * (N2 - 2)) + (N1 + (N2 - 1))

N1 = numberInput1;
N2 = numberInput2;

// side number
SN = N1 + (N2 - 1);
// inside number
IN = N1 * (N2 - 2);
// multiplied number
MN = IN + SN;
<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>Multiplication Table Additive Pairs</title>
    <meta name="description" content="calculates the multiplication table's additive pairs">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="styles.css">
</head>

<body>
    <form
        action="/Kinda Useful Programs/Text-based/Math-based/Calculations/Calculating Multiplication Table Additive Pairs/index.html"
        method="GET">
        <label for="input number">Please enter a number:</label>
        <input type="text" name="input number 1" id="numberInput1" required>
        <input type="text" name="input number 2" id="numberInput2" required>
        <button type="submit">Submit</button>
    </form>
    <script src="script.js" async defer></script>
</body>

</html>

这是您可以做到的一种方式。它不会创建全局变量,但是如果你真的需要全局变量,你可以修改一下。

//  DOM nodes to assist proof of concept
const input1   = document.getElementById('numberInput1');
const input2   = document.getElementById('numberInput2');
const output1  = document.querySelector('output[for="numberInput1"]');
const output2  = document.querySelector('output[for="numberInput2"]');
const outputSN = document.querySelector('output#sn');
const outputIN = document.querySelector('output#in');
const outputMN = document.querySelector('output#mn');

//  here is the meat of your logic
const recalculateDerivedValues = () => {
  const N1 = input1.valueAsNumber;
  const N2 = input2.valueAsNumber;
  const IN = N1 * (N2 - 2);
  const SN = N1 + (N2 - 1);
  outputSN.innerText = SN;
  outputIN.innerText = IN;
  outputMN.innerText = IN + SN;
};

//  attach event handlers so values are updated in real time
const watchThenUpdate = (watchedElement, updateElement) => {
  watchedElement.addEventListener('input', ev => {
    updateElement.innerText = ev.target.value;
    recalculateDerivedValues();
  });
};


//  attach event handlers to respective elements
watchThenUpdate(input1, output1);
watchThenUpdate(input2, output2);
output {
  font-size: x-large;
  font-weight: bold;
  display: block;
  font-family: Courier New;
  color: maroon;
}

output::before {
  content: attr(id);
  padding-right: 1em;
  text-transform: uppercase;
}
<form action="#" id="f" name="f">
  <label for="input number">Please enter two numbers:</label>
  <input type="number" name="input number 1" id="numberInput1" required>
  <input type="number" name="input number 2" id="numberInput2" required>
</form>

<output form="f" for="numberInput1" id="N1"></output>
<output form="f" for="numberInput2" id="N2"></output>

<output id="sn"></output>
<output id="in"></output>
<output id="mn"></output>