これはjqueryで解答しているのを見ましたが、私はJavaScriptでどのように行うことができますか?これまでのところ、私は次のことを考え出しましたが、nameF.valueを正しくキャプチャしていないことを理解しています。field.valueが空のときに入力ボタンを無効にする
var nameF = document.getElementById("name-field");
var formButton = document.getElementById("form-button");
function go() {
if (nameF.value == "") {
formButton.classList.add("notclickable");
}
}
go();
#form-button {
cursor: pointer;
outline: none;
border: none;
font-family: $body-font;
font-size: 14px;
padding: 16px;
color: #fff;
font-weight: 800;
background-color: #000;
position: relative;
border-radius: 2px;
opacity: 1;
}
#form-button.notclickable {
opacity: .1;
pointer-events: none;
}
<input type="text" name="name" class="form-style form-field" id="name-field" placeholder="Your name">
<input type="submit" value="Send" id="form-button">
https://stackoverflow.com/questions/ask#
私はバニラを使用して、jQueryのせずに、それをどのように行うことができますか?
ありがとうございました!
あなたが探している行動に依存します。ページが読み込まれ、ユーザが入力を行う前に 'go()'を一度呼び出すだけです。 – charlietfl
'.keyup'イベントは' button'要素の 'disabled'属性をトグルします。 –