将Js变量更改为<input>中的用户输入值有问题。

huangapple go评论54阅读模式
英文:

having trouble changing Js variable to user input value from <input>

问题

以下是翻译的内容:

// Variables
const button = document.getElementById('button');
const returnChange = document.getElementById('return-change');
const returnStatus = document.getElementById('return-status');
const cid = [
  ['PENNY', 1.01],
  ['NICKEL', 2.05],
  ['DIME', 3.1],
  ['QUARTER', 4.25],
  ['ONE', 90],
  ['FIVE', 55],
  ['TEN', 20],
  ['TWENTY', 60],
  ['ONE HUNDRED', 100]
];

let cash = document.getElementById('payment');
let price = document.getElementById('price').value;

console.log(price);
console.log(cash);
console.log(cid);
<div id="price-div">
    <form>
        <label for="price">Purchase Price:
            <input id='price' name='price' type="number" required />
        </label>
    </form>
</div>

<div id="payment-div">
    <label for="payment"> Payment Amount:
        <input id='payment' type="number" required />
    </label>
</div>
<div id="button-div">
    <button id="button" type="button">Return Change</button>
</div>
<div id="change">
    <p id="return-change"></p>
    <p id="return-status"></p>
</div>

请注意,我已经将HTML和JavaScript代码部分翻译成中文,但没有翻译代码中的变量名和标签属性名。如果您有任何其他问题,请随时提出。

英文:

There is a user input field in an HTML form. My intention is for the user's inputed value to become the value of the variable 'price'. I have been unable to figure out how to do this. Can someone please steer me in the right direction.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

// Variables
const button = document.getElementById(&#39;button&#39;);
const returnChange = document.getElementById(&#39;return-change&#39;);
const returnStatus = document.getElementById(&#39;return-status&#39;);
const cid = [
  [&#39;PENNY&#39;, 1.01],
  [&#39;NICKEL&#39;, 2.05],
  [&#39;DIME&#39;, 3.1],
  [&#39;QUARTER&#39;, 4.25],
  [&#39;ONE&#39;, 90],
  [&#39;FIVE&#39;, 55],
  [&#39;TEN&#39;, 20],
  [&#39;TWENTY&#39;, 60],
  [&#39;ONE HUNDRED&#39;, 100]
];

let cash = document.getElementById(&#39;payment&#39;);
let price = document.getElementById(&#39;price&#39;).value;


  console.log(price);
  console.log(cash);
  console.log(cid);

<!-- language: lang-html -->

        &lt;div id=&quot;price-div&quot;&gt;
            &lt;form&gt;
                &lt;label for=&quot;price&quot; &gt;Purchase Price:
                    &lt;input id=&#39;price&#39; name=&#39;price&#39; type=&quot;number&quot; required /&gt;
                &lt;/label&gt;
            &lt;/form&gt;
        &lt;/div&gt;

        &lt;div id=&quot;payment-div&quot;&gt;
            &lt;label for=&quot;payment&quot;&gt; Payment Amount:
                &lt;input id=&#39;payment&#39; type=&quot;number&quot; required /&gt;
            &lt;/label&gt;
        &lt;/div&gt;
        &lt;div id=&quot;button-div&quot;&gt;
            &lt;button id=&quot;button&quot; type=&quot;button&quot; &gt;Return Change&lt;/button&gt;
        &lt;/div&gt;
        &lt;div id=&quot;change&quot;&gt;
            &lt;p id=&quot;return-change&quot;&gt;&lt;/p&gt;
            &lt;p id=&quot;return-status&quot;&gt;&lt;/p&gt;
        &lt;/div&gt;

<!-- end snippet -->

答案1

得分: 1

你可以使用addEventListener来实现:

const priceInput = document.getElementById("price");
const priceHandler = e => {
  price = e.target.value;
  console.log(price);
};
priceInput.addEventListener('input', priceHandler);
<form>
  <label for="price">购买价格:
    <input id='price' name='price' type="number" required />
  </label>
</form>

<details>
<summary>英文:</summary>

You might want to use an [addEventListener][1]:

&lt;!-- begin snippet: js hide: false console: true babel: false --&gt;

&lt;!-- language: lang-js --&gt;

    const priceInput = document.getElementById(&quot;price&quot;);
    const priceHandler = e =&gt; {
      price = e.target.value;
      console.log(price);
    };
    priceInput.addEventListener(&#39;input&#39;, priceHandler);

&lt;!-- language: lang-html --&gt;

    &lt;form&gt;
      &lt;label for=&quot;price&quot;&gt;Purchase Price:
               &lt;input id=&#39;price&#39; name=&#39;price&#39; type=&quot;number&quot; required /&gt;
           &lt;/label&gt;
    &lt;/form&gt;

&lt;!-- end snippet --&gt;


  [1]: https://developer.mozilla.org/fr/docs/Web/API/EventTarget/addEventListener

</details>



# 答案2
**得分**: -1

我们需要更多关于您尝试上述操作时看到的情况的详细信息。然而,从代码来看,有一些建议:
1) 您不需要"return getValue()"。仅使用"getValue()" 即可。
2) 您可以从返回的元素中获取值

```javascript
function getValue() {
  let element = document.getElementById("price");
  let value = element.value;
  console.log(value)
}

<form onchange="getValue()">
  <label for="price">购买价格:
    <input id='price' name='price' type="number" required />
  </label>
</form>
英文:

We need a little more detail on what you are seeing happen when you try the above. However, looking at the code, some suggestions:

  1. You don't need the 'return getValue()'. getValue() alone is fine.
  2. You can get the value from the element returned

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

function getValue() {
  let element = document.getElementById(&quot;price&quot;);
  let value = element.value;
  console.log(value)
}

<!-- language: lang-html -->

&lt;form onchange=&quot;getValue()&quot;&gt;
  &lt;label for=&quot;price&quot;&gt;Purchase Price:
       &lt;input id=&#39;price&#39; name=&#39;price&#39; type=&quot;number&quot; required /&gt;
   &lt;/label&gt;
&lt;/form&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年3月7日 01:18:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/75653878.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定