suchen

Heim  >  Fragen und Antworten  >  Hauptteil

Wenn das erforderliche Element von select2 false ist, ist jquery ungültig

<p>Wie verwende ich jQuery, um die erforderlichen Felder von select2 auf false zu setzen? Ich möchte jQuery verwenden, um jedes select2 zu require=false zu machen und es zu deaktivieren. </p> <p>Ich habe es versucht...aber $('#accodeformheader').find('input, select').attr('required', false); funktioniert nicht. Gibt es eine Möglichkeit, select2s „required“ = false zu machen? </p> <pre class="brush:php;toolbar:false;">$(document).ready(function() { $('#idtask').change(function() { var taskVal = $(this).val(); console.log("Aktueller Aufgabenwert", taskVal); if (taskVal === "Grass") { document.getElementById("idspeakerdetailsdiv").style.display = "block"; } anders { 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 acode = $(this).attr("id"); console.log(accode) $.getJSON("{{url_for('karya_bp.getonespeakerdetails')}}", { asycaccesscode:String(accode) }, Funktion(Daten) { console.log(Daten) metadata = data.speakerdetails.current.workerMetadata console.log(Metadaten) $('#accesscode').attr('value', accode)</pre> <pre class="brush:php;toolbar:false;"><div id="accodeformheader" style="display: block;"> <div class="form-group"> <label for="idaccesscodefor">Zugriffscode für:</label><br> <select class="accesscodefor" id="idaccesscodefor" name="accesscodefor" style="width:55%" erforderlich></select><br> </div> <div class="form-group"> <label for="idtask">Aufgabe:</label><br> <select class="task" id="idtask" name="task" style="width:55%" erforderlich></select><br> </div> <div id="uploadaccode" style="display: block;"></div> </div> <div class="form-group"> <label class="col-form-label">Name:</label> <input type="text" class="form-control" id="idname" name="sname" placeholder="--Speaker Name--" style="width:55%" value="" erforderlich > </div> <div class="form-group"> <label for="sagegroup">Altersgruppe: </label> <br> <select class="age" id="idage" name="sagegroup" style="width:55%" erforderlich> </select><br> </div> <div id="idspeakerdetailsdiv" style="display: none;"> <div class="form-group"> <label for="sgender">Geschlecht: </label> <br> <select class="gender" id="idgender" name="sgender" style="width:55%" erforderlich> </select><br> </div> <div class="form-group"> <label for="educationalevel">Bildungsniveau: </label> <br> <select class="educationlvl" id="idelevel" name="educationalevel" style="width:55%" erforderlich> </select><br> </div> <div class="form-group"> <label for="moe12">Medium Of Education (bis 12.): </label><br> <select class="educationmediumupto12" id="idmediumpre" name="moe12" multiple="multiple" style="width:55%" erforderlich> </select><br> </div> <div class="form-group"> <label for="moea12">Bildungsmedium (nach dem 12.): </label><br> <select class="educationmediumafter12" id="idmediumpost" name="moea12" multiple="multiple" style="width:55%" erforderlich> </select><br> </div> </div> <button type="button" id="closebutton" class="btn btn-secondary" data-dismiss="modal">Schließen</button> <input type="submit" value="Submit"> <br><br> </form> </div></pre></p>
P粉775788723P粉775788723454 Tage vor639

Antworte allen(1)Ich werde antworten

  • P粉762730205

    P粉7627302052023-09-04 00:50:24

    如果您不需要,可以简单地删除“required”属性

    $('select').removeAttr('required');

    如果您想重新设置,请按照以下步骤进行:

    $('select').prop('required', 'required');

    如果您想根据id或类删除特定的select2下拉菜单,请按照以下步骤进行:

    $('#your_id_name').removeAttr('required');

    使用类名:

    $('.your_class_name').removeAttr('required');

    同样,您可以根据id和类进行设置,如下所示:

    $('#your_id_name').prop('required', 'required');

    使用类名:

    $('.your_class_name').prop('required', 'required');

    我希望我理解您的问题并给出了正确的答案。

    Antwort
    0
  • StornierenAntwort