嗨,我想使用 jquery 删除“必需 =””属性。
<input type="text" id="edit-submitted-first-name" name="submitted[first_name]" value="" size="30" maxlength="128" required="" >
Just:
$('#edit-submitted-first-name').removeAttr('required');
If you're interested in further reading take a look here.
document.querySelector('#edit-submitted-first-name').required = false;
$('#edit-submitted-first-name').removeAttr('required');
If you want to set required to true
true
$(document).ready(function(){ $('#edit-submitted-first-name').prop('required',true); });
if you want to set required to false
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>