无法在HTML表单和Javascript中显示结果。

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

Can't get Result to show with HTML Forms and Javascript

问题

我尝试让我的结果显示在文本框输入框中,按照我的任务要求,但我无法让它们显示出来。数学部分根本不显示,所以我的主要问题是无法让代码显示在框中。

document.querySelector("#buttonS").addEventListener("click", function(e) {
  if (document.querySelector("#gallons").reportValidity()) {
    let gallons = document.querySelector("#gallons").value;

    if (document.querySelector("#quarts").checked) {
      quartsTotal = gallons * 4;
      document.querySelector("#quartsResult").placeholder = `quartsTotal`);
    } else if (document.querySelector("#pints").checked) {
      // ...
    } else if (document.querySelector("#cups").checked) {
      // ...
    }
  }
});
<form id="bakimg">
  <input type="number" step=".01" min="0" id="gallons" required><label for="gallons">  你有多少加仑的牛奶?</label>
  <br>
  <br>
  <label for="conversion">你想要哪种转换?</label><br>
  <input type="radio" value="quarts" name="gallonsC" checked><label for="quarts">夸脱</label>
  <input type="radio" value="pints" name="gallonsC"><label for="pints">品脱</label>
  <input type="radio" value="cups" name="gallonsC"><label for="cups">杯子</label>
</form>
<br>
<button type="button" id="buttonS">提交</button><br>
<h1>结果</h1>
<br>
<input type="text" id="quartsResult" placeholder=""><label for="quartsResult">夸脱</label><br>
<input type="text" id="pintsResult"><label for="pintsResult">品脱</label><br>
<input type="text" id="cupsResult"><label for="cupsResult">杯子</label>
</div>
英文:

I'm trying to get my results to show up in the text box inputs as per my assignment but I can't get them to show up at all. The math isn't showing up at all so my big issue is that I can't get the code to show up in the box.

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

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

document.querySelector.(&quot;#buttonS&quot;).addEventListener(&quot;click&quot;, function(e) {
  if (document.querySelector(&quot;#gallons&quot;).reportValidity()) {
    let gallons = document.querySelector(&quot;#gallons&quot;).value;

    if (document.querySelector(&quot;#quarts&quot;).checked) {
      quartsTotal = gallons * 4;
      document.querySelector(&quot;#quartsResult&quot;).placeholder = `quartsTotal`);

  } else if (document.querySelector(&quot;#pints&quot;).checked) {

  } else if (document.querySelector(&quot;#cups&quot;).checked) {

  }

}

});

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

&lt;form id=&quot;bakimg&quot;&gt;
  &lt;input type=&quot;number&quot; step=&quot;.01&quot; min=&quot;0&quot; id=&quot;gallons&quot; required&gt;&lt;label for=&quot;gallons&quot;&gt;  How many gallons of milk do you have?&lt;/label&gt;
  &lt;br&gt;
  &lt;br&gt;
  &lt;label for=&quot;conversion&quot;&gt;Which conversion would you like?&lt;/label&gt;&lt;br&gt;
  &lt;input type=&quot;radio&quot; value=&quot;quarts&quot; name=&quot;gallonsC&quot; checked&gt;&lt;label for=&quot;quarts&quot;&gt;Quarts&lt;/label&gt;
  &lt;input type=&quot;radio&quot; value=&quot;pints&quot; name=&quot;gallonsC&quot;&gt;&lt;label for=&quot;pints&quot;&gt;Pints&lt;/label&gt;
  &lt;input type=&quot;radio&quot; value=&quot;cups&quot; name=&quot;gallonsC&quot;&gt;&lt;label for=&quot;cups&quot;&gt;Cups&lt;/label&gt;
&lt;/form&gt;
&lt;br&gt;
&lt;button type=&quot;button&quot; id=&quot;buttonS&quot;&gt;Submit&lt;/button&gt;&lt;br&gt;
&lt;h1&gt;Results&lt;/h1&gt;
&lt;br&gt;
&lt;input type=&quot;text&quot; id=&quot;quartsResult&quot; placeholder=&quot;&quot;&gt;&lt;label for=&quot;quartsResult&quot;&gt;Quarts&lt;/label&gt;&lt;br&gt;
&lt;input type=&quot;text&quot; id=&quot;pintsResult&quot;&gt;&lt;label for=&quot;pintsResult&quot;&gt;Pints&lt;/label&gt;&lt;br&gt;
&lt;input type=&quot;text&quot; id=&quot;cupsResult&quot;&gt;&lt;label for=&quot;cupsResult&quot;&gt;Cups&lt;/label&gt;
&lt;/div&gt;

<!-- end snippet -->

答案1

得分: 2

  • 检查你的语法并进行以下更改:
document.querySelector(&quot;#buttonS&quot;)

应该写成这样:

document.querySelector(&quot;#buttonS&quot;) // querySelector 后面不需要 .
  • 检查额外的括号:
document.querySelector(&quot;#quartsResult&quot;).placeholder = `quartsTotal`); // &lt;-- 移除结尾的括号
  • 在 HTML 输入元素(quarts、pints、cups)中添加适当的 ID:
&lt;input type=&quot;radio&quot; value=&quot;quarts&quot; name=&quot;gallonsC&quot; checked id=&quot;quarts&quot;&gt;
&lt;input type=&quot;radio&quot; value=&quot;pints&quot; name=&quot;gallonsC&quot; id=&quot;pints&quot;&gt;
&lt;input type=&quot;radio&quot; value=&quot;cups&quot; name=&quot;gallonsC&quot; id=&quot;cups&quot;&gt;
  • 移除反引号以使用变量值(否则 quartsTotal 仍然是字符串):
`quartsTotal` -> quartsTotal
// 或许这是你的意思:
`${quartsTotal}`

祝你在任务中好运!

英文:

Check your syntax and make the following changes:

  • Check the browser console for errors and use the appropriate syntax:
document.querySelector.(&quot;#buttonS&quot;)

should be written like this:

document.querySelector(&quot;#buttonS&quot;) // No . after querySelector
  • Check the extra parentheses:
document.querySelector(&quot;#quartsResult&quot;).placeholder = `quartsTotal`); // &lt;-- Remove the closing parens
  • Add the proper IDs to the HTML input elements (quarts, pints, cups):
&lt;input type=&quot;radio&quot; value=&quot;quarts&quot; name=&quot;gallonsC&quot; checked id=&quot;quarts&quot;&gt;
&lt;input type=&quot;radio&quot; value=&quot;pints&quot; name=&quot;gallonsC&quot; id=&quot;pints&quot;&gt;
&lt;input type=&quot;radio&quot; value=&quot;cups&quot; name=&quot;gallonsC&quot; id=&quot;cups&quot;&gt;
  • Remove the backticks in order to use the variable value (otherwise quartsTotal is still a string):
`quartsTotal` -&gt; quartsTotal
// Perhaps this is what you meant:
`${quartsTotal}`

Good luck with the assignment!

huangapple
  • 本文由 发表于 2023年2月19日 06:45:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/75496844.html
匿名

发表评论

匿名网友

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

确定