使用 jQuery 删除 html5 必需属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13951336/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Removing html5 required attribute with jQuery
提问by LeBlaireau
Hi I would like to remove the 'required=""' attribute with jquery.
嗨,我想用 jquery 删除 'required=""' 属性。
<input
type="text"
id="edit-submitted-first-name"
name="submitted[first_name]"
value=""
size="30"
maxlength="128"
required=""
>
回答by Minko Gechev
回答by pala?н
Using Javascript:
使用 JavaScript:
document.querySelector('#edit-submitted-first-name').required = false;
Using jQuery:
使用jQuery:
$('#edit-submitted-first-name').removeAttr('required');
回答by Latief Anwar
If you want to set required to true
如果你想设置 required 到 true
$(document).ready(function(){
$('#edit-submitted-first-name').prop('required',true);
});
if you want to set required to false
如果你想设置为 required false
$(document).ready(function(){
$('#edit-submitted-first-name').prop('required',false);
});
回答by Nijin P J
$('#id').removeAttr('required');?????
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
回答by Aris
Even though the ID selector is the simplest, you can also use the name selector as below:
即使 ID 选择器是最简单的,您也可以使用名称选择器,如下所示:
$('[name='submitted[first_name]']').removeAttr('required');
For more see: https://api.jquery.com/attribute-equals-selector/