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

时间:2017-07-21 07:08:22

标签: javascript forms


我正在为表格寻找我的终极互动。我希望禁用提交按钮,直到最后一个必需的form-element有一个值 但它需要具体(我将在我写的一篇文章中使用此代码)。 我想要每个onblur(移动到表单字段之外)来检查是否还有具有“invalid”属性的字段(必需字段没有值或者字段由于不匹配模式而具有无效条目)。
如果只剩下一个字段(因此您正在或即将进入最后一个必需字段),我想检查按键(在最后一个必填字段中)该字段是否已变为“有效” ”。如果是这样,则提交按钮(type = submit)应该已删除“已禁用”。 我在香草JS中更喜欢这个,但是Jquery都是最好的。 : - )

1 个答案:

答案 0 :(得分:0)

所以,我给了它一个刺。得到了这个东西。可能会更干净。我仍然更喜欢vanilla JS,但我找不到合适的答案。

所以这是我的代码:

$( "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>