简体   繁体   English

如何禁用按钮,直到仅使用 JavaScript 填充所有输入字段?

[英]How to disable a button until all the input fields are filled using only JavaScript?

I am creating a quiz game, but I only want the user to be able to submit if all of the input fields are filled out using only JavaScript.我正在创建一个问答游戏,但我只希望用户能够提交所有输入字段是否仅使用 JavaScript 填写。 I saw other similar questions on StackOverflow that used jquery, but I want to only use JavaScript.我在 StackOverflow 上看到了使用 jquery 的其他类似问题,但我只想使用 JavaScript。

html (Django template) html(Django 模板)

{% for q in quiz_questions %}
  <h3 class="quiz_questions">{{q.1}}
    <input type="text" id="id_question{{q.0}}" class="input">
  </h3>
  <div id="div{{ q.0 }}"></div>
  <br>
  <br>
  {% endfor %}
  <input type="submit" value="Submit" class="button quiz_button"
         id="{{ quiz.id }}" disabled="true">

current JavaScript当前 JavaScript

document.addEventListener('DOMContentLoaded', function(){
  enableChecking();
})

function enableChecking(){
  button = document.getElementsByClassName("quiz_button");
  if (document.getElementsByClassName("input").value === "") {
    button.disabled = true;
  } else {
    button.disabled = false;
  }
}

If you have a <form> wrapped around everything (which is very simple to do if you don't), add required to all of the <input> that'll prevent the <form> from being submitted should there be any blank <input> s.如果你有一个<form>包裹着所有东西(如果你没有,这很简单),添加required到所有<input>这将阻止<form>如果有任何空白<input> s。

 input { display: block; }
 <form> <input required value='XXXXXXXXXXX'> <input required value='XXXXXXXXXXX'> <input required value='XXXXXXXXXXX'> <input required value='XXXXXXXXXXX'> <input required value='XXXXXXXXXXX'> <input required value='XXXXXXXXXXX'> <input required> <button>Submit</button> </form>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Javascript禁用按钮,直到所有字段都填满 - Javascript disable button until all fields are filled 禁用表单提交按钮,直到填充所有输入/文本区域字段,而不使用 for 循环(无 jQuery) - Disable form submit button until all input/textarea fields are filled, without using for loop (no jQuery) 禁用提交按钮,直到填写输入字段 - Disable submit button until input fields are filled 在 ReactJS 中填写所有输入字段之前,如何禁用表单提交按钮? - How to disable form submit button until all input fields are filled in ReactJS? 如何在填写所有输入字段之前禁用表单提交按钮?! ReactJS ES2015 - How to disable form submit button until all input fields are filled?! ReactJS ES2015 如何禁用提交按钮,直到使用 html 和香草 js 填写所有必填字段 - How to disable submit button until all mandatory fields are filled using html and vanilla js 如何禁用按钮,直到不使用JavaScript(仅HTML5和CSS)填充必填字段 - How to Disable button until required fields are filled without using javascript, just HTML5 and CSS 如何在字段填满条目之前禁用按钮 - How to disable a button until fields are filled with entries 禁用“提交”按钮,直到“输入”字段中填写了RAILS - Disable Submit button until Input fields filled in RAILS Javascript:如何在所有字段都经过验证之前禁用提交按钮? - Javascript: How to disable submit button until all fields are validated?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM