英文:
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('username');
var button = document.getElementById('saveScoreBtn');
if (input.value !== '') {
button.disabled = true;
} else {
button.disabled = false;
}
}
<!-- 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 -->
答案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('username');
var button = document.getElementById('saveScoreBtn');
if (input.value !== '') {
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 -->
<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 -->
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 <form>
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 -->
<form class="end-form-container" onsubmit="handleSubmit(event)">
<h2 id="end-text">Enter your name below to save your score!</h2>
<input type="text" name="name" id="username"
placeholder="Enter your name!" required/>
<button type="submit" class="btn" id="saveScoreBtn">
Save
</button>
</form>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论