小编All*_*dez的帖子

在Chrome中使用JavaScript删除必需属性时,checkValidity()会产生false

除非我遗漏了什么,否则这似乎是Chrome中的一个错误.

这里有三种形式,每种形式有两个无线电元素 如果没有required属性,则表单上的checkValidity()将返回true,如预期的那样.当存在必需属性时,表单上的checkValidity()将返回false,如预期的那样.

但是,当使用JavaScript删除所需的属性时,checkValidity()将返回false.这不是我所期望的.任何解释/解决方法赞赏!这在Safari和Firefox中正常工作,在Chrome中不起作用.

console.log('Valid form: ' + document.getElementById('validForm').checkValidity());
console.log('Invalid form: ' + document.getElementById('invalidForm').checkValidity());
//document.getElementById('rad1').required = false; //Neither this nor below work
//document.getElementById('rad2').required = false;
document.getElementById('rad1').removeAttribute('required');
document.getElementById('rad2').removeAttribute('required');
console.log('Should be valid form: ' + document.getElementById('shouldBeValidForm').checkValidity());
Run Code Online (Sandbox Code Playgroud)
<form id="validForm">
  <input type="radio" name="my radio 1" value="option 1">
  <input type="radio" name="my radio 1" value="option 2">
</form>
<form id="invalidForm">
  <input type="radio" name="my radio 2" value="option 1" required>
  <input type="radio" name="my radio 2" value="option 2" required>
</form>
<form id="shouldBeValidForm">
  <input id="rad1" type="radio" name="my radio 3" value="option 1" …
Run Code Online (Sandbox Code Playgroud)

javascript forms html5

1
推荐指数
1
解决办法
2949
查看次数

标签 统计

forms ×1

html5 ×1

javascript ×1