简体   繁体   中英

Disable all the <select> items in JavaScript

I have a few <select> s items in my JSP file, what I want is to disable all the <select> s items.

This is my JSP file:

<select id="comboSuperGrupos1" name="combosSupG">
    <option value="0">N...</option>
    <!-- X OPTIONS -->
</select>

<select id="comboSuperGrupos2" name="combosSupG">
    <option value="0">N...</option>
    <!-- X OPTIONS -->
</select>

This is what I'm trying:

var combosSuperGrupo = document.getElementsByName("combosSupG");
combosSuperGrupo.disabled = true;

But I can't achieve my goal. I'm thinking to do that with jQuery, but I don't know if I can mix JavaScript with jQuery.

Any question post on comments.

 const combosSuperGrupo = document.getElementsByName("combosSupG"); combosSuperGrupo.forEach(_ => _.disabled = true); 
 <select id="comboSuperGrupos1" name="combosSupG"> <option value="0">N...</option> <!-- X OPTIONS --> </select> <select id="comboSuperGrupos2" name="combosSupG"> <option value="0">N...</option> <!-- X OPTIONS --> </select> 

jQuery :

$('select[name="combosSupG"]').attr('disabled',true);

JavaScript :

var selects = document.querySelectorAll("select[name='combosSupG']");
selects.forEach(function(s){
    s.disabled = true;
});

Yes you can and it's easier to code:

This is how to disable every select with that name:

 $("select[name='combosSupG']").prop('disabled', true); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <select id="comboSuperGrupos1" name="combosSupG"> <option value="0">N...</option> <!-- X OPTIONS --> </select> <select id="comboSuperGrupos2" name="combosSupG"> <option value="0">N...</option> <!-- X OPTIONS --> </select> 

This is how to disable every select in the page:

 $("select").prop('disabled', true); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <select id="comboSuperGrupos1" name="combosSupG"> <option value="0">N...</option> <!-- X OPTIONS --> </select> <select id="comboSuperGrupos2" name="combosSupG"> <option value="0">N...</option> <!-- X OPTIONS --> </select> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM