简体   繁体   English

为什么我的提交按钮不能正常工作?

[英]Why does my submit button not work properly?

I have a form created using HTML and I have a submit button.我有一个使用 HTML 创建的表单,并且有一个提交按钮。
I am trying to have the submit button disabled if the fields are not filled out.如果字段未填写,我试图禁用提交按钮。
However, now even if the fields are filled out the button stays disabled.但是,现在即使字段已填写,按钮仍处于禁用状态。

 const subButton = document.querySelector('.sbutton') const inputTexts = document.querySelector('.input') subButton.disabled = true function clickedButton() { if (document.querySelector('.input').value === "") { subButton.disabled = true } else { subButton.disabled = false } }
 <div class="form"> <section> <form action=[link] method="POST"> <fieldset> <legend>Your Contact Info?</legend> <label class="firstname" for="firstname">First Name</label> <input class="input" type="text" id="firstname" name="name" placeholder="Your first name" required> <label class="lastname" for="lastname">Last Name</label> <input type="text" id="lastname" name="lname" placeholder="Your last name"> <label class="email" for="email">E-mail</label> <input class="input" type="email" id="email" name="email" placeholder="Your e-mail" required> <label class="comments" for="comments">Additional Comments</label> <textarea class="input" placeholder="Anything else we should know!" id="comments" name="comments" required></textarea> <label class='input' for="timeofday">When is the best time of day to contact you?</label> <select id="timeofday" name="timeofday"> <option value='Morning'>Morning</option> <option selected value="afternoon">Afternoon</option> <option value="evening">Evening</option> </select> </fieldset> <button class="sbutton" type="submit">Submit</button>

The issue is, that you never call the function clickedButton() .问题是,您永远不会调用 function clickedButton clickedButton() You need an eventListener that listens for change events within the inputs and then calls that function:您需要一个 eventListener 来侦听输入中的更改事件,然后调用它 function:

const INPUTS = document.querySelectorAll('input');
INPUTS.forEach(el =>
  el.addEventListener('change', clickedButton)
)

 const subButton = document.querySelector('.sbutton') const inputTexts = document.querySelector('.input') const INPUTS = document.querySelectorAll('input'); subButton.disabled = true INPUTS.forEach(el => el.addEventListener('change', clickedButton) ) function clickedButton() { if (document.querySelector('.input').value === "") { subButton.disabled = true } else { subButton.disabled = false } }
 <div class="form"> <section> <form action=[link] method="POST"> <fieldset> <legend>Your Contact Info?</legend> <label class="firstname" for="firstname">First Name</label> <input class="input" type="text" id="firstname" name="name" placeholder="Your first name" required> <label class="lastname" for="lastname">Last Name</label> <input type="text" id="lastname" name="lname" placeholder="Your last name"> <label class="email" for="email">E-mail</label> <input class="input" type="email" id="email" name="email" placeholder="Your e-mail" required> <label class="comments" for="comments">Additional Comments</label> <textarea class="input" placeholder="Anything else we should know!" id="comments" name="comments" required></textarea> <label class='input' for="timeofday">When is the best time of day to contact you?</label> <select id="timeofday" name="timeofday"> <option value='Morning'>Morning</option> <option selected value="afternoon">Afternoon</option> <option value="evening">Evening</option> </select> </fieldset> <button class="sbutton" type="submit">Submit</button>

Note, that you check only the first input.请注意,您只检查第一个输入。 querySelector returns only 1 element (the first). querySelector只返回一个元素(第一个)。

Nothing on the page is calling clickedButton()页面上没有任何内容调用 clickedButton()

Attach an EventListener for changes on the form that calls this function.在调用此 function 的表单上附加一个用于更改的 EventListener。

var form = document.querySelector('form');
form.addEventListener('change', clickedButton);

Also when you are using querySelector you will only get the first element in the form which is a <input... .此外,当您使用 querySelector 时,您只会获得表单中的第一个元素<input... If you want to check for all you should use querySelectorAll.如果你想检查所有你应该使用 querySelectorAll。

But that function would return an array so then you need to check if all of them have text.但是 function 会返回一个数组,所以你需要检查它们是否都有文本。

In that case you could do it like this.在那种情况下,你可以这样做。

function validateForm() {
  // Get all elements
  const elements = document.querySelectorAll("input");
  // Filter out elements that have no value, if there is more then 1 set disabled to true, else it is false
  const disabled = elements.filter((el) => el.value === "").length > 0 ? true : false;

  subButton.disabled = disabled
}

And if you are going with this way you need to call this function instead of the other one like this:如果您采用这种方式,则需要调用此 function 而不是另一个,如下所示:

var form = document.querySelector('form');
form.addEventListener('change', validateForm);

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM