我似乎无法使用我的JavaScript函数禁用一个HTML按钮。

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

I can't seem to disable a HTML button using my Javascript function

问题

我在HTML中创建了一个按钮,目前处于禁用状态,但应该在特定的文本框不再为空后变为启用状态。然后,该按钮将在另一个网页上保存从先前完成的游戏中获得的分数和名称。我通过将按钮和文本框链接到一个JavaScript文件并指定一个函数来实现这一点。然而,即使文本框填充了内容,按钮仍然拒绝启用。

这是我尝试的代码:
你建议我做些什么改变?

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

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

function checkInput() {
  var input = document.getElementById('username');
  var button = document.getElementById('saveScoreBtn');

  if (input.value !== '') {
    button.disabled = false;
  } else {
    button.disabled = true;
  }
}

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

<form class="end-form-container">
  <h2 id="end-text">Enter your name below to save your score!</h2>
  <input type="text" name="name" id="username"
      placeholder="Enter your name!" onkeyup="checkInput()" />
  <button class="btn" id="saveScoreBtn" type="submit"
      onclick="saveBtn(event)" disabled>
    Save
  </button>
</form>

<!-- end snippet -->
英文:

I created a button in HTML that is currently disabled but is supposed to become enabled after a particular textbox is found to no longer be empty. Then, the button saves the name with the score from the previously completed game on another web page. I did this by linking the button and textbox to a JavaScript file and specifying a function to do this for me. However, the button is refusing to become enabled even after the textbox is filled.

This is the code I tried:
What do you suggest I change?

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

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

function checkInput() {
  var input = document.getElementById(&#39;username&#39;);
  var button = document.getElementById(&#39;saveScoreBtn&#39;);

  if (input.value !== &#39;&#39;) {
    button.disabled = true;
  } else {
    button.disabled = false;
  }
}

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

&lt;form class=&quot;end-form-container&quot;&gt;
  &lt;h2 id=&quot;end-text&quot;&gt;Enter your name below to save your score!&lt;/h2&gt;
  &lt;input type=&quot;text&quot; name=&quot;name&quot; id=&quot;username&quot;
      placeholder=&quot;Enter your name!&quot; onkeyup=&quot;checkInput()&quot; /&gt;
  &lt;button class=&quot;btn&quot; id=&quot;saveScoreBtn&quot; type=&quot;submit&quot;
      onclick=&quot;saveBtn(event)&quot; disabled&gt;
    Save
  &lt;/button&gt;
&lt;/form&gt;

<!-- end snippet -->

答案1

得分: 2

看起来你可能在 JavaScript 函数 checkInput 中错误地交换了条件。如果输入不为空 (input.value !== ''), 你将按钮设置为禁用,这与你想实现的相反。尝试将条件交换如下:

function checkInput() {
  var input = document.getElementById('username');
  var button = document.getElementById('saveScoreBtn');

  if (input.value !== '') {
    button.disabled = false; // 当输入不为空时按钮应该可用
  } else {
    button.disabled = true; // 当输入为空时按钮应该禁用
  }
}

另外,请确保你的 JavaScript 代码正确链接到你的 HTML 文件,并且没有错误阻止 checkInput 函数运行。如果有任何错误,你应该在浏览器的 JavaScript 控制台中看到相应的错误信息。

尝试这样做,然后在评论中告诉我结果。

英文:

It looks like you might have mistakenly switched your conditions in your if-else statement in the JavaScript function checkInput. You set the button to be disabled if the input is not empty (input.value !== ''), which is the opposite of what you're trying to achieve. Try switching the conditions like so:

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

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

function checkInput() {
  var input = document.getElementById(&#39;username&#39;);
  var button = document.getElementById(&#39;saveScoreBtn&#39;);

  if (input.value !== &#39;&#39;) {
    button.disabled = false; // button should be enabled when input is not empty
  } else {
    button.disabled = true; // button should be disabled when input is empty
  }
}

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

&lt;form class=&quot;end-form-container&quot;&gt;
  &lt;h2 id=&quot;end-text&quot;&gt;Enter your name below to save your score!&lt;/h2&gt;
  &lt;input type=&quot;text&quot; name=&quot;name&quot; id=&quot;username&quot; placeholder=&quot;Enter your name!&quot; onkeyup=&quot;checkInput()&quot; /&gt;
  &lt;button class=&quot;btn&quot; id=&quot;saveScoreBtn&quot; type=&quot;submit&quot; onclick=&quot;saveBtn(event)&quot; disabled&gt;
    Save
  &lt;/button&gt;
&lt;/form&gt;

<!-- end snippet -->

Also, ensure that your JavaScript code is properly linked to your HTML file and that there are no errors preventing the checkInput function from running. You should see errors in your web browser's JavaScript console if there are any.

Try this and let me know in comments.

答案2

得分: 0

不需要实现自定义表单验证。它已经内置在 <form> 元素中。

function handleSubmit(e) {
  e.preventDefault(); // 阻止页面导航
  
  console.log(e.target.elements.username.value); // 值...
}
<form class="end-form-container" onsubmit="handleSubmit(event)">
  <h2 id="end-text">在下面输入您的姓名以保存您的分数!</h2>
  <input type="text" name="name" id="username"
      placeholder="输入您的姓名!" required/>
  <button type="submit" class="btn" id="saveScoreBtn">
    保存
  </button>
</form>
英文:

No need to implement custom form validation. It's baked-into the &lt;form&gt; element.

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

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

function handleSubmit(e) {
  e.preventDefault(); // Prevent page navigation
  
  console.log(e.target.elements.username.value); // Value...
}

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

&lt;form class=&quot;end-form-container&quot; onsubmit=&quot;handleSubmit(event)&quot;&gt;
  &lt;h2 id=&quot;end-text&quot;&gt;Enter your name below to save your score!&lt;/h2&gt;
  &lt;input type=&quot;text&quot; name=&quot;name&quot; id=&quot;username&quot;
      placeholder=&quot;Enter your name!&quot; required/&gt;
  &lt;button type=&quot;submit&quot; class=&quot;btn&quot; id=&quot;saveScoreBtn&quot;&gt;
    Save
  &lt;/button&gt;
&lt;/form&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年6月9日 02:02:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/76434571.html
匿名

发表评论

匿名网友

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

确定