使用jQuery删除html5必需属性


77

嗨,我想用jquery删除'required =“”'属性。

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

Answers:



36

如果要设置为 true

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

如果要设置为 false

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

2
做到这一点的最佳方法。我尝试了其他解决方案,但这对我有很大帮助。thx
ankit suthar


7

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


By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.