select2的必填项为false时,jquery无效
P粉775788723
2023-09-03 10:06:25
<p>如何使用jQuery使select2的必填字段为false?我想使用jQuery使每个select2都要求= false并将其禁用。</p>
<p>我尝试了这个......但是$('#accodeformheader').find('input, select').attr('required', false);不起作用。
有没有办法使select2的“required”= false?</p>
<pre class="brush:php;toolbar:false;">$(document).ready(function() {
$('#idtask').change(function() {
var taskVal = $(this).val();
console.log("当前任务值", taskVal);
if (taskVal === "Grass") {
document.getElementById("idspeakerdetailsdiv").style.display = "block";
}
else {
document.getElementById("idspeakerdetailsdiv").style.display = "none";
}
})
$('.lexemeview').click(function() {
$('#accodeformheader').find('input, select').attr('disabled', true);
$('#accodeformheader').find('input, select').attr('required', false);
document.getElementById("accodeformheader").style.display = "none";
buttonType = $(this).attr("id")
activeform(buttonType)
$('#formdisplay').find('input, select').attr('disabled', true);
$('#editbutton').attr('hidden', false);
var accode = $(this).attr("id");
console.log(accode)
$.getJSON("{{url_for('karya_bp.getonespeakerdetails')}}", {
asycaccesscode:String(accode)
}, function(data) {
console.log(data)
metadata = data.speakerdetails.current.workerMetadata
console.log (metadata)
$('#accesscode').attr('value', accode)</pre>
<pre class="brush:php;toolbar:false;"><div id="accodeformheader" style="display: block;">
<div class="form-group">
<label for="idaccesscodefor">Access Code For:</label><br>
<select class="accesscodefor" id="idaccesscodefor" name="accesscodefor" style="width:55%" required></select><br>
</div>
<div class="form-group">
<标签=“idtask”>任务:标签>
<select class="task" id="idtask" name="task" style="width:55%" required></select><br>
<div id="uploadaccode" style="display: block;"></div>
<div class="form-group">
<标签类=“col-form-label”>名称:标签>
<div class="form-group">
<标签=“sagegroup”>年龄组:标签> <br>
<选择 class="age" id="idage" name="sagegroup" style="width:55%" 必需>;
</选择><br>
如果您不需要,可以简单地删除“required”属性
如果您想重新设置,请按照以下步骤进行:
如果您想根据id或类删除特定的select2下拉菜单,请按照以下步骤进行:
使用类名:
同样,您可以根据id和类进行设置,如下所示:
使用类名:
我希望我理解您的问题并给出了正确的答案。