我正在尝试为某些字段未填写在表单中的事件设置button
状态disabled
,但由于某种原因脚本不起作用
let userName = document.getElementById("name");
let userEmail = document.getElementById("email");
let button = document.getElementById("send");
function checkForm() {
if (userName === false || userEmail === false) {
button.disabled = true;
} else {
button.disabled = false;
}
}
checkForm();
form {
display: flex;
flex-direction: column;
width: 200px;
}
input,
select {
margin-bottom: 10px;
}
<form action="/">
<input type="text" placeholder="Ваше имя" id="name">
<input type="text" placeholder="Эл. почта" id="email">
<select name="city">
<option selected disabled>Выберите город</option>
<option value="Москва">Москва</option>
<option value="Санкт-Петербург">Санкт-Петербург</option>
<option value="Екатеринбург">Екатеринбург</option>
<option value="Новосибирск">Новосибирск</option>
</select>
<button id="send">Отправить</button>
</form>