使用 jQuery 删除 html5必需属性

嗨,我想使用 jquery 删除“必需 =””属性。

<input
type="text"
id="edit-submitted-first-name"
name="submitted[first_name]"
value=""
size="30"
maxlength="128"
required=""
>
166899 次浏览

Just:

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

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

Using Javascript:

document.querySelector('#edit-submitted-first-name').required = false;

Using jQuery:

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

If you want to set required to true

$(document).ready(function(){
$('#edit-submitted-first-name').prop('required',true);
});

if you want to set required to false

$(document).ready(function(){
$('#edit-submitted-first-name').prop('required',false);
});

Even though the ID selector is the simplest, you can also use the name selector as below:

$('[name='submitted[first_name]']').removeAttr('required');

For more see: https://api.jquery.com/attribute-equals-selector/

$('#id').removeAttr('required');​​​​​
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>