Как отключить кнопку sbumit, пока не введено максимальное слово - PullRequest
0 голосов
/ 27 апреля 2020

Я нахожусь в процессе обучения кодированию и мало знаю о JavaScript, поэтому у меня возникли некоторые проблемы и я буду признателен за любую помощь сообщества. У меня есть форма ввода и я хочу отключить кнопку отправки, пока не введено максимальное количество слов. У меня есть мой код как:

<script>
    // Get reference to textbox
    var input = document.getElementById("words");

    // Add event handler for event that can be cancelled and prevent 
    // excessive data from ever getting into the textbox
    input.addEventListener("keypress", function(evt) {
        // Get value of textbox and split into array where there is one or 
        // more continuous spaces
        var words = this.value.split(/\s+/);
        // Get # of words in array
        var numWords = words.length;
        var maxWords = 8;
        // If we are at the limit and the key pressed wasn't BACKSPACE or DELETE,
        // don't allow any more input
        if(numWords > maxWords) {
            evt.preventDefault(); // Cancel event
        }
</script>

<form action="" method="post "id="myform">
  <div class="form-group">
    <input id="words"  data-max-words="8" data-announce="true" 
           class="words-data-form-control" type="text" name="wordlist" 
           wordlistlength="8"autocomplete="off" autocorrect="off" 
           autocapitalize="off" spellcheck="false" required />
  </div>
  <div class="feedback-container">
    <div class="feedback" style="display: none;"></div>
  </div>
  <div class="form-group">
    <button class="btn btn-primary text-capitalize float-right" 
        id="submit" type="submit" 
        style="color: rgb(255,255,255);background-color: rgb(12, 108, 242);" 
        disabled>
      <strong>submit</strong>
    </button>
  </div>
</form>

1 Ответ

0 голосов
/ 27 апреля 2020

Вы можете удалить атрибут disabled из элемента button с помощью JavaScript:

let button = document.getElementById("submit");
button.removeAttribute("disabled");

Если вы хотите снова отключить кнопку, вы можете сделать это:

let button = document.getElementById("submit");
button.setAttribute("disabled", "");
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...