【发布时间】:2020-02-19 10:37:32
【问题描述】:
我已经为我的保存按钮设置了“禁用”属性,我的目标是在用户填写表单中的所有输入字段时启用它。
现在它可以工作了,因为我在最后一个输入字段中设置了onkeyup="checkForm(this)",但这不是解决我的问题的聪明方法。
这是我表单中的 html 代码:
div class="page" id="create__book__formular">
<div class="page__formular">
<h3 id="formualer__name">Formular</h3>
<label>Name:
<input type="text" placeholder="Name" id="title" >
</label>
<label>Autor:
<input type="text" placeholder="Autor" id="autor">
</label>
<label>ISBN:
<input type="text" placeholder="ISBN" id="isbn">
</label>
<label>Anazhl:
<input type="text" placeholder="Anazhl" id="number">
</label>
<label>Verlag:
<input type="text" onkeyup="checkForm(this)" placeholder="Verlag" id="publishing">
</label>
<button type="button" class="btn btn-success create__book__formular" id="save--button" disabled="disabled">
save
</button>
<button type=" button " class="btn btn-light create__book__formular" id="back--button">
back
</button>
</div>
</div>
这是我的 JavaScript 代码:
function checkForm(create__book__formular) {
var bt = document.getElementById('save--button');
if (create__book__formular.value != '') {
bt.disabled = false;
} else {
bt.disabled = true;
}
}
感谢您的帮助!
【问题讨论】:
-
你可以使用onChange事件,因为它会在你离开输入框时触发一次函数
-
请注意,如果按钮被禁用,仍然可以提交表单,方法是在输入时按 enter。
标签: javascript html disable