使用 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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 13:07:43  来源:igfitidea点击:

Removing html5 required attribute with jQuery

jqueryattributes

提问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

Just:

只是:

$('#edit-submitted-first-name').removeAttr('required');?????

If you're interested in further reading take a look here.

如果您有兴趣进一步阅读,请看这里

回答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/

更多信息请参见:https: //api.jquery.com/attribute-equals-selector/