javascript - 允许按钮被按下一次
问题描述
我有一个这样的按钮:
<fieldset>
<input type="text" placeholder="Color" name="Color" required>
</fieldset>
<fieldset>
<input type="text" placeholder="Bird" name="Bird" required>
</fieldset>
<fieldset>
<button name="submit" type="submit" id="submit">Lagre</button>
</fieldset>
仅当文本字段具有值时才会提交表单。但是如果用户向“提交”按钮发送垃圾邮件并按下它 5 次,表单将被提交 5 次。第一次按下后如何禁用按钮?我知道document.getElementById("submit").disabled = true;
,但是如果用户忘记在“Bird”中输入值并按下按钮,用户将无法再次按下它。有小费吗?
解决方案
在您的提交功能中,检查输入字段是否已填写。如果没有,则显示警报。满足此条件后,您可以禁用该按钮。
JSFiddle:https ://jsfiddle.net/guv12f83/
function submitForm() {
const color = document.getElementById('colorInput').value.trim();
const bird = document.getElementById('birdInput').value.trim();
if (color.length > 0 && bird.length > 0) {
document.getElementById('submit').disabled = true
console.log('Enter your code here')
} else {
alert('Please fill all fields')
}
}
推荐阅读
- apache - 每天 22:51 使用 cron 自动重启 apache 服务
- python - 访问模板中的 ModelForm 查询集对象字段
- python - 从数据框列中的文本块中解析地址
- google-cloud-pubsub - 批处理谷歌发布/订阅事件,然后将集合发布为单个事件 - Apache Beam
- javascript - How to filter an array that excludes from object's property array
- firebase - InvalidStateError:在不允许突变的数据库上尝试了突变操作
- jenkins - 在 stable/jenkins helm 图表中将 jenkins tmp 文件夹定义为持久性卷
- python - Discord.py 删除服务器中的每个频道
- javascript - 传单中是否有任何功能可以在特定角度(例如:30 度)上绘制图层(标记或圆圈)?
- mysql - 带有函数的 MySQL 索引