简体   繁体   English

在 javascript 上要求 html5 返回 true 后,如何禁用提交按钮?

[英]How can I disable submit button after required html5 return true on the javascript?

My html code like this:我的 html 代码是这样的:

<form>
    <label for="name">* Name</label>
    <input type="text" id="name" required><br>
    <label for="name">* Address</label>
    <input type="text" id="address" required><br>
    <input type="submit" value="Submit">
</form>

I'm using required HTML5 attribute to validate fields.我正在使用required的 HTML5 属性来验证字段。

If user does not input text, it will display HTML5 error.如果用户没有输入文本,它会显示 HTML5 错误。

I want to disable the submit button if any of the required fields are left empty.如果任何必填字段留空,我想禁用提交按钮。

How can I do it?我该怎么做?

You can disable the pointer events on the button with CSS. 您可以使用CSS禁用按钮上的指针事件。

 form:invalid>#submit { pointer-events: none; } 
 <form> <label for="name">* Name</label> <input type="text" id="name" required><br> <label for="name">* Address</label> <input type="text" id="address" required><br> <input type="submit" value="Submit" id="submit"> </form> 

You could also disable the button using Javascript. 您也可以使用Javascript禁用该按钮。

 function disableField() { const invalidForm = document.querySelector('form:invalid'); const submitBtn = document.getElementById('submit'); if (invalidForm) { submitBtn.setAttribute('disabled', true); } else { submitBtn.disabled = false; } } disableField(); const inputs = document.getElementsByTagName("input"); for (let input of inputs) { input.addEventListener('change', disableField); } 
 <form> <label for="name">* Name</label> <input type="text" id="name" required><br> <label for="name">* Address</label> <input type="text" id="address" required><br> <input type="submit" value="Submit" id="submit"> </form> 

你可以做到之后

$("#button").prop("disabled", true);

You can try something like this: 您可以尝试如下操作:

$(function() {
$("#input-text").keyup(function () {
 if($("#input-text")[0].checkValidity()){
      $('#submit-button').attr('disabled', 'disabled');
 }else{
        $('#submit-button').removeAttr('disabled');
 }
 });
})($);

<form id="newRecord">
<input type="text" required id="input-text"/>
<button form="newRecord" type="submit" id="submit-
button">Submit</button>
</form>

An more generic solution for multiple forms and supporting multiple inputs and buttons.一种更通用的解决方案,适用于多种形式并支持多种输入和按钮。

It detects the change event directly on forms individually, and then change the disabled attribute for all submit types它直接在表单上单独检测更改事件,然后更改所有提交类型的禁用属性

 function disableFormSubmit(form) { const buttons = form.querySelectorAll( 'button[type="submit"], input[type="submit"]' ); const disableButtons = () => buttons.forEach(el => (el.disabled = form.matches(":invalid"))); form.addEventListener("change", disableButtons); disableButtons(); } document.querySelectorAll("form").forEach(disableFormSubmit);
 <form action="/"> <legend>First form</legend> <input type="text" required> <br> <input type="Checkbox" required> required checkbox <br> <input type="submit"> <button type="submit">Button submit</button> </form> <hr> <form action="/"> <legend>Second form</legend> <select required> <option value="" disabled selected>Select one</option> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> </select> <br> <input type="radio" required name="radio"> required radio <br> <input type="submit"> <button type="submit">Button submit</button> </form>

Personally i would go on an solution using only CSS as a visual cue, unless you need the disabled attribute就我个人而言,除非您需要 disabled 属性,否则我会继续使用仅 CSS 作为视觉提示的解决方案

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

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