📌  相关文章
📜  如果输入为空,则禁用提交按钮 - Javascript 代码示例

📅  最后修改于: 2022-03-11 15:04:06.989000             🧑  作者: Mango

代码示例1
document.getElementById('submitID').disabled = true; //Disable at first

//Listen for keyup event (after we hit a key)
//-----------------------------------------------------------
//NOTE: We can also use the change event, but the submit button
//      won't update until we finish typing the whole thing
//-----------------------------------------------------------
document.getElementById('myInputFieldID').addEventListener('keyup', e => {
  //Check for the input's value
  if (e.target.value == "") {
    document.getElementById('submitID').disabled = true;
  }
  else {
    document.getElementById('submitID').disabled = false;
  }
});

//Happy coding :)