简体   繁体   English

没有更多无效字段时启用禁用按钮

[英]enable disabled button when there are no more invalid fields


I'm looking for my ultimate interaction for a form. 我正在寻找表单的最终交互方式。 I would like the submit button to be disabled until the last required form-element has an value. 我希望在最后一个必需的表单元素具有值之前禁用提交按钮。
But it needs to be specific (I will use this code in an article I'm writing). 但这需要具体(我将在撰写的文章中使用此代码)。
I want with each onblur (move outside the form field) to check whether there are still fields which have an "invalid" attribute (either the required field has no value or an field has invalid entry due to mismatch pattern). 我想与每个onblur(移到表单字段之外)检查是否仍然存在具有“无效”属性的字段(由于不匹配模式,必填字段没有值或字段具有无效条目)。
If there is only one field left (so you're on or about to enter the last required field), I want to check on keystroke (in that last required field) whether the field has become "valid". 如果仅剩一个字段(因此您正在或即将输入最后一个必填字段),我想检查击键(在最后一个必填字段中)该字段是否已变为“有效”。 If so, the submit-button (type = submit) should have "disabled" removed. 如果是这样,则应该删除“禁用”提交按钮(类型=提交)。
I would prefer this in vanilla JS, but both with Jquery would be best. 我更喜欢在香草JS中使用,但最好同时使用Jquery。 :-) :-)

So, I gave it a stab. 所以,我给了它一个刺。 Got this thing working. 这个东西工作了。 Could probably be more clean. 可能更干净。 And I still would prefer vanilla JS, but I couldn't find the right answers. 而且我仍然希望使用Vanilla JS,但是找不到正确的答案。

So this is my code: 这是我的代码:

 $( "form *:invalid" ).on( "blur", function( event ) { var invalidFields = document.querySelectorAll("form *:invalid"); var invFieldLength = invalidFields.length; function getInvalidElements() { if (invFieldLength == 1) { $( ":invalid" ).on( "change", function( event ) { console.log("laatste"); $("button[type=submit]").prop('disabled', false); }); }; if (invFieldLength >= 1) { console.log("weer disabled"); $("button[type=submit]").prop('disabled', true); }; } console.log(invalidFields); console.log(invFieldLength); getInvalidElements("form *:invalid"); }); function firstInvalidField() { var invalidFields = document.querySelectorAll("form *:invalid"); var invFieldLength = invalidFields.length; $(invalidFields[0]).focus(); console.log(invalidFields); }; 
 form { font-family: "Open Sans"; width: 40em; max-width: 100%; margin: 2em auto 2em; padding: 2.5rem; display: flex; flex-direction: column; align-items: flex-start; border-radius: 1rem; background-color: #eee; } label { margin: 1em .5em .2em; font-size: 1rem; font-weight: 600; vertical-align: text-bottom; line-height: 1.4; flex: 1 0 auto; text-align: right; } input, select, textarea { padding: .3rem .5em; box-sizing: border-box; font-size: 1.2rem; flex: 1 0 auto; width: 100%; line-height: 1; height: 2.5em; border: 3px solid #aaa; border-radius: .5em; } input:focus, select:focus, textarea:focus { border: 3px solid #333; } textarea { height: 4em; } button { position: relative; margin: .5em 2rem .5em auto; padding: .3em 1em; border-radius: .5em; font-size: 2em; max-width: 5em; } button > span { padding: .666rem; font-size: 1.5rem; display: none; color: #000; opacity: .1; width: 175%; } button > span:hover, button > span:focus { opacity: 1; transition: opacity .5s ease-in-out; } button[disabled] { cursor: not-allowed; color: #666; } button[disabled]:hover > span, button[disabled]:focus > span { position: absolute; padding: .5em; border: 3px solid #ffeb8c; border-radius: .5em; background-color: #eee; right: -1rem; top: -1rem; display: block; } button[disabled] > span a { cursor: pointer; color: blue; } button[disabled] > span a:hover, button[disabled] > span a:focus { text-decoration: underline; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <form> <label for="name">First name</label><input id="name" type="text" pattern="[a-zA-Z]"> <label for="lastname">Last name</label><input id="lastname" type="text" required pattern="[a-zA-Z]"> <label for="desires">Desires in life</label><textarea id="desires" type="text" required></textarea> <label for="greatest">Greatest number in the world (under 3)</label><select id="greatest" type="text" required> <option selected value="">select something</option> <option>1</option> <option>2</option> </select> <label for="lastname">Last name</label><input id="lastname" type="text"> <button type="submit" disabled><span>Sorry, you still have to fill in some fields before you can submit.<br><br><a onclick="firstInvalidField()">Lets go to the first field you need to fill or has an error...</a></span>Submit</button> </form> 

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

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