当不需要输入时,Bootstrap表单验证以绿色突出显示标签文本?

我的Bootstrap 4 form validation发生了一件奇怪的事情,其中​​两个单选按钮在验证表单时将其标签的前景更改为绿色.如果表单无效,则标签不会更改为红色.无论哪种方式,它们都应保留其黑色前景,因为单选按钮输入不像我的其他表单元素那样具有与之关联的必需属性.他们为什么会变成绿色?我如何防止这两个事件或停止在这两个实体上进行表单验证?

index.html

<form id="signup-form" novalidate>

<div class="form-group">
  <h1>Sign Up</h1>
  <p>Use this form to enter your name and email to sign up.</p>
</div>

<!-- Name -->
<div class="form-group">
  <div class="row">
    <div class="col">
      <label for="firstNameInput">Name</label>
      <input type="text" class="form-control" id="firstNameInput" placeholder="First name" required>
      <div class="valid-feedback">
        Looks good!
      </div>
    </div>
    <div class="col">
      <label for="lastNameInput">Last</label>
      <input type="text" class="form-control" id="lastNameInput" placeholder="Last name" required>
      <div class="valid-feedback">
        Looks good!
      </div>
    </div>
  </div>
</div>

<!-- Email -->
<div class="form-group">
  <label for="emailAddressInput">Email address</label>
  <input type="email" class="form-control" id="emailAddressInput" aria-describedby="emailHelp" placeholder="name@example.com" required>
  <small id="emailHelp" class="form-text text-muted">We'll never share your email with anyone else.</small>
  <div class="invalid-feedback">
    Please provide a valid email.
  </div>
</div>

<!-- Options -->
<div class="form-group">
  <div class="form-check">
    <input class="form-check-input" type="radio" name="Radios" id="type1RadioButton" value="option1" checked>
    <label class="form-check-label" for="type1RadioButton">
      Type 1 user
    </label>
  </div>
  <div class="form-check">
    <input class="form-check-input" type="radio" name="Radios" id="type2RadioButton" value="option2">
    <label class="form-check-label" for="type2RadioButton">
      Type 2 user
    </label>
  </div>
</div>

<!-- Submit Button -->
<div class="form-group">
  <a href="#" class="btn btn-primary" id="submitButton" type="submit">Submit</a>
</div>

</form>

script.js

$(document).ready(function() {
$("#submitButton").click(function() {

  //Fetch form to apply custom Bootstrap validation
  var form = $("#signup-form")
  alert(form.prop('id')) //test to ensure calling form correctly

  if (form[0].checkValidity() === false) {
    event.preventDefault()
    event.stopPropagation()
    }
    form.addClass('was-validated')
  })
})

结果

表单验证之前

当不需要输入时,Bootstrap表单验证以绿色突出显示标签文本?

表单验证后

当不需要输入时,Bootstrap表单验证以绿色突出显示标签文本?

解决方法:

使用HTML5验证表单时,:valid状态(或验证失败时为:invalid)将应用于所有表单输入.我不知道一种防止:valid状态应用于特定输入的方法.

将:valid应用于输入后,Bootstrap将使用此CSS,使标签变为绿色…

.was-validated .form-check-input:valid~.form-check-label {
    color: #28a745;
}

要覆盖Bootstrap应用的绿色样式,请使用标签上的text-dark类.这样,标签将始终为黑色,而不是绿色(:valid)或红色(:invalid).

https://www.codeply.com/go/gvR4ArUPKL

  <div class="form-check">
       <input class="form-check-input" type="radio" name="Radios" id="type2RadioButton" value="option2">
       <label class="form-check-label text-dark" for="type2RadioButton">
          Type 2 user
       </label>
  </div> 
上一篇:java-播放2.3验证Json请求主体


下一篇:验证字段输入是否为字符串laravel