这是文档:HTMLFieldSetElement - Web APIs | MDN。
我想知道fieldset
有效性意味着什么。这是否意味着所有inputs
的有效性,例如,在fieldset
内?如果输入超出范围的值,为什么以下工作不起作用:
var form = document.querySelector('form');
form.querySelector('button').addEventListener('click', function() {
var fieldsets = form.querySelectorAll('fieldset');
for (var fieldset of fieldsets) {
var output = fieldset.querySelector('output');
if (fieldset.validity.valid) {
output.value = 'Valid!';
} else {
output.value = 'Invalid!';
}
}
});
input {
width: 4em;
}
input:invalid {
outline: 1px solid red;
}
<form>
<fieldset>
<input type="number" step="any" min="0" max="100">
<input type="number" step="any" min="0" max="100">
<input type="number" step="any" min="0" max="100">
<output></output>
</fieldset>
<fieldset>
<input type="number" step="any" min="0" max="100">
<input type="number" step="any" min="0" max="100">
<input type="number" step="any" min="0" max="100">
<output></output>
</fieldset>
<button type="button">Validate</button>
</form>
https://dev.w3.org/html5/pf-summary/forms.html#the-fieldset-element
约束验证:始终禁止字段集元素进行约束验证。
https://dev.w3.org/html5/pf-summary/forms.html#barred-from-constraint-validation
列出的与表单相关的元素是约束验证的候选元素,除非条件禁止元素进行约束验证。 (例如,如果元素是输出或字段集元素,则禁止其进行约束验证。)
https://developer.mozilla.org/en-US/docs/Web/API/HTMLFieldSetElement#Methods
HTMLFieldSetElement.reportValidity()始终返回true,因为对象永远不是约束验证的候选对象。
你可以这样做:
var form = document.querySelector('form');
form.querySelector('button').addEventListener('click', function() {
var fieldsets = form.querySelectorAll('fieldset');
for (var fieldset of fieldsets) {
var output = fieldset.querySelector('output');
output.value = fieldset.querySelectorAll("input:invalid").length===0 ? 'Valid!' : 'Invalid!';
}
});
input {
width: 4em;
}
input:invalid {
outline: 1px solid red;
}
<form>
<fieldset>
<input type="number" step="any" min="0" max="100">
<input type="number" step="any" min="0" max="100">
<input type="number" step="any" min="0" max="100">
<output></output>
</fieldset>
<fieldset>
<input type="number" step="any" min="0" max="100">
<input type="number" step="any" min="0" max="100">
<input type="number" step="any" min="0" max="100">
<output></output>
</fieldset>
<button type="button">Validate</button>
</form>
或循环:
var errors = {
"valueMissing": "Missing",
"typeMismatch": "Wrong type",
"patternMismatch": "Wrong pattern",
"tooLong": "Too long",
"tooShort": "Too short",
"rangeUnderflow": "< @",
"rangeOverflow": "> @",
"stepMismatch": "Step error",
"badInput": "Not a number",
"customError": ""
}
function getError(inp) {
for (var val in inp.validity) {
if (inp.validity[val]) {
var error = errors[val];
if (val === "rangeUnderflow") error = error.replace("@",inp.min)
if (val === "rangeOverflow") error = error.replace("@",inp.max)
return error;
}
}
}
var form = document.querySelector('form');
form.querySelector('button').addEventListener('click', function() {
var fieldsets = form.querySelectorAll('fieldset');
for (var fieldset of fieldsets) {
var output = fieldset.querySelector('output');
output.value = 'Valid!';
var inputs = fieldset.querySelectorAll("input");
var errors = []
for (var inp of inputs) {
if (!inp.validity.valid) {
errors.push(getError(inp));
}
}
if (errors.length) output.value=errors.join(" and ")
}
});
input {
width: 4em;
}
input:invalid {
outline: 1px solid red;
}
<form>
<fieldset>
<input type="number" step="any" min="0" max="100">
<input type="number" step="any" min="0" max="100">
<input type="number" step="any" min="0" max="100">
<output></output>
</fieldset>
<fieldset>
<input type="number" step="any" min="0" max="100">
<input type="number" step="any" min="0" max="100">
<input type="number" step="any" min="0" max="100">
<output></output>
</fieldset>
<button type="button">Validate</button>
</form>