必需的属性在 Chrome 中不起作用,但在 FireFox 中起作用
The required attribute is not working in Chrome but does work in FireFox
我正在使用 bootstrap 进行样式设置,但我没有定义任何我自己的样式 类。由于 FireFox 正在运行,我相信我使用的结构是正确的。但是Chrome并不开心,对两者的细微差别了解不够,无法追根究底。 'State' 下拉列表的必需属性在两者中都不起作用,但我认为这是另一个问题。
我看到一篇文章描述了当应用程序 url 是 ssl 时 Chrome 如何不加载非 ssl 的脚本,但这不应该适用,因为我正在使用 ng serve 和我的 url 不安全。否则,所有与 required 属性不起作用相关的文章都与 FireFix 上的故障有关,这与我遇到的情况相反。
FireFox: 86.0.1 (64-bit)
Chrome: 89.0.4389.90 (Official Build) (64-bit)
来自组件html
<div class="row">
<div class="col-md-5 mb-3">
<label for="{{idPrefix}}City">City</label>
<input type="text" class="form-control" id="{{idPrefix}}City" placeholder="" required [(ngModel)]="model.City">
</div>
<div class="col-md-4 mb-3">
<label for="{{idPrefix}}state">State</label>
<select class="custom-select d-block w-100" id="{{idPrefix}}state" required [(ngModel)]="model.State">
<option value="">Choose...</option>
<option *ngFor="let State of StateArray" value="{{State.abbreviation}}">{{State.name}}</option>
</select>
</div>
<div class="col-md-3 mb-3">
<label for="{{idPrefix}}zip">Zip</label>
<input type="text" class="form-control" id="{{idPrefix}}zip" placeholder="" required [(ngModel)]="model.Zip">
</div>
</div>
index.html
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css">
通常 chrome 不会为无效输入创建此红色边框。
你可以添加一些 css 来手动创建边界:
input:required {
border-color: #800000;
border-width: 3px;
}
input:invalid {
background-color: #ffdddd;
}
input:valid {
background-color: #ddffdd;
}
但老实说,我不知道单独使用 bootsrap 会怎么做。
检查下面的代码,它适用于所有浏览器
<!doctype html>
<html lang="en">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<!-- Bootstrap CSS -->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
<title>Test</title>
</head>
<body>
<form class="needs-validation" novalidate>
<div class="form-row">
<div class="col-md-4 mb-3">
<label for="validationCustom01">First name</label>
<input type="text" class="form-control" id="validationCustom01" placeholder="First name" value="Mark" required>
<div class="valid-feedback">
Looks good!
</div>
</div>
<div class="col-md-4 mb-3">
<label for="validationCustom02">Last name</label>
<input type="text" class="form-control" id="validationCustom02" placeholder="Last name" value="Otto" required>
<div class="valid-feedback">
Looks good!
</div>
</div>
<div class="col-md-4 mb-3">
<label for="validationCustomUsername">Username</label>
<div class="input-group">
<div class="input-group-prepend">
<span class="input-group-text" id="inputGroupPrepend">@</span>
</div>
<input type="text" class="form-control" id="validationCustomUsername" placeholder="Username" aria-describedby="inputGroupPrepend" required>
<div class="invalid-feedback">
Please choose a username.
</div>
</div>
</div>
</div>
<div class="form-row">
<div class="col-md-6 mb-3">
<label for="validationCustom03">City</label>
<input type="text" class="form-control" id="validationCustom03" placeholder="City" required>
<div class="invalid-feedback">
Please provide a valid city.
</div>
</div>
<div class="col-md-3 mb-3">
<label for="validationCustom04">State</label>
<input type="text" class="form-control" id="validationCustom04" placeholder="State" required>
<div class="invalid-feedback">
Please provide a valid state.
</div>
</div>
<div class="col-md-3 mb-3">
<label for="validationCustom05">Zip</label>
<input type="text" class="form-control" id="validationCustom05" placeholder="Zip" required>
<div class="invalid-feedback">
Please provide a valid zip.
</div>
</div>
</div>
<div class="form-group">
<div class="form-check">
<input class="form-check-input" type="checkbox" value="" id="invalidCheck" required>
<label class="form-check-label" for="invalidCheck">
Agree to terms and conditions
</label>
<div class="invalid-feedback">
You must agree before submitting.
</div>
</div>
</div>
<button class="btn btn-primary" type="submit">Submit form</button>
</form>
<script>
// Example starter JavaScript for disabling form submissions if there are invalid fields
(function() {
'use strict';
window.addEventListener('load', function() {
// Fetch all the forms we want to apply custom Bootstrap validation styles to
var forms = document.getElementsByClassName('needs-validation');
// Loop over them and prevent submission
var validation = Array.prototype.filter.call(forms, function(form) {
form.addEventListener('submit', function(event) {
if (form.checkValidity() === false) {
event.preventDefault();
event.stopPropagation();
}
form.classList.add('was-validated');
}, false);
});
}, false);
})();
</script>
</body>
<!-- jQuery first, then Popper.js, then Bootstrap JS -->
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script>
</body>
</html>
您可以参考以下文档:https://getbootstrap.com/docs/4.0/components/forms/?#validation
我正在使用 bootstrap 进行样式设置,但我没有定义任何我自己的样式 类。由于 FireFox 正在运行,我相信我使用的结构是正确的。但是Chrome并不开心,对两者的细微差别了解不够,无法追根究底。 'State' 下拉列表的必需属性在两者中都不起作用,但我认为这是另一个问题。
我看到一篇文章描述了当应用程序 url 是 ssl 时 Chrome 如何不加载非 ssl 的脚本,但这不应该适用,因为我正在使用 ng serve 和我的 url 不安全。否则,所有与 required 属性不起作用相关的文章都与 FireFix 上的故障有关,这与我遇到的情况相反。
FireFox: 86.0.1 (64-bit)
Chrome: 89.0.4389.90 (Official Build) (64-bit)
来自组件html
<div class="row">
<div class="col-md-5 mb-3">
<label for="{{idPrefix}}City">City</label>
<input type="text" class="form-control" id="{{idPrefix}}City" placeholder="" required [(ngModel)]="model.City">
</div>
<div class="col-md-4 mb-3">
<label for="{{idPrefix}}state">State</label>
<select class="custom-select d-block w-100" id="{{idPrefix}}state" required [(ngModel)]="model.State">
<option value="">Choose...</option>
<option *ngFor="let State of StateArray" value="{{State.abbreviation}}">{{State.name}}</option>
</select>
</div>
<div class="col-md-3 mb-3">
<label for="{{idPrefix}}zip">Zip</label>
<input type="text" class="form-control" id="{{idPrefix}}zip" placeholder="" required [(ngModel)]="model.Zip">
</div>
</div>
index.html
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css">
通常 chrome 不会为无效输入创建此红色边框。 你可以添加一些 css 来手动创建边界:
input:required {
border-color: #800000;
border-width: 3px;
}
input:invalid {
background-color: #ffdddd;
}
input:valid {
background-color: #ddffdd;
}
但老实说,我不知道单独使用 bootsrap 会怎么做。
检查下面的代码,它适用于所有浏览器
<!doctype html>
<html lang="en">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<!-- Bootstrap CSS -->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
<title>Test</title>
</head>
<body>
<form class="needs-validation" novalidate>
<div class="form-row">
<div class="col-md-4 mb-3">
<label for="validationCustom01">First name</label>
<input type="text" class="form-control" id="validationCustom01" placeholder="First name" value="Mark" required>
<div class="valid-feedback">
Looks good!
</div>
</div>
<div class="col-md-4 mb-3">
<label for="validationCustom02">Last name</label>
<input type="text" class="form-control" id="validationCustom02" placeholder="Last name" value="Otto" required>
<div class="valid-feedback">
Looks good!
</div>
</div>
<div class="col-md-4 mb-3">
<label for="validationCustomUsername">Username</label>
<div class="input-group">
<div class="input-group-prepend">
<span class="input-group-text" id="inputGroupPrepend">@</span>
</div>
<input type="text" class="form-control" id="validationCustomUsername" placeholder="Username" aria-describedby="inputGroupPrepend" required>
<div class="invalid-feedback">
Please choose a username.
</div>
</div>
</div>
</div>
<div class="form-row">
<div class="col-md-6 mb-3">
<label for="validationCustom03">City</label>
<input type="text" class="form-control" id="validationCustom03" placeholder="City" required>
<div class="invalid-feedback">
Please provide a valid city.
</div>
</div>
<div class="col-md-3 mb-3">
<label for="validationCustom04">State</label>
<input type="text" class="form-control" id="validationCustom04" placeholder="State" required>
<div class="invalid-feedback">
Please provide a valid state.
</div>
</div>
<div class="col-md-3 mb-3">
<label for="validationCustom05">Zip</label>
<input type="text" class="form-control" id="validationCustom05" placeholder="Zip" required>
<div class="invalid-feedback">
Please provide a valid zip.
</div>
</div>
</div>
<div class="form-group">
<div class="form-check">
<input class="form-check-input" type="checkbox" value="" id="invalidCheck" required>
<label class="form-check-label" for="invalidCheck">
Agree to terms and conditions
</label>
<div class="invalid-feedback">
You must agree before submitting.
</div>
</div>
</div>
<button class="btn btn-primary" type="submit">Submit form</button>
</form>
<script>
// Example starter JavaScript for disabling form submissions if there are invalid fields
(function() {
'use strict';
window.addEventListener('load', function() {
// Fetch all the forms we want to apply custom Bootstrap validation styles to
var forms = document.getElementsByClassName('needs-validation');
// Loop over them and prevent submission
var validation = Array.prototype.filter.call(forms, function(form) {
form.addEventListener('submit', function(event) {
if (form.checkValidity() === false) {
event.preventDefault();
event.stopPropagation();
}
form.classList.add('was-validated');
}, false);
});
}, false);
})();
</script>
</body>
<!-- jQuery first, then Popper.js, then Bootstrap JS -->
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script>
</body>
</html>
您可以参考以下文档:https://getbootstrap.com/docs/4.0/components/forms/?#validation