javascript - How to disable <select> based on other <select> on Select2.js -
i'm having little trouble disabling "select" input.
i need disable "cidade" whenever nothing selected on "estado".
here's code:
<select id="estado" class="select_customized"> <option></option> <option value="sp">são paulo</option> <option value="mg">minas gerais</option> <option value="rj">rio de janeiro</option> </select> <select id="cidade" class="select_customized"> <option></option> <option value="sao-paulo">são paulo</option> <option value="minas-gerais">minas gerais</option> <option value="rio-de-janeiro">rio de janeiro</option> </select> and here's script i'm using:
$("#cidade").select2("enable", false); $("#estado").on('change',function(){ var is_empty = $('#estado').is(":empty"); if(is_empty){ $("#cidade").select2("enable", false); } else { $("#cidade").select2("enable", true); } }); this kinda works, whenever choose on "estado" input, , clear again, "cidade" not disable again... suggestions?
you want use select2 call check if selection box empty, instead of going original #estado box created (since hidden select2).
your call:
var is_empty = $('#estado').is(":empty"); then can changed like:
var is_empty = $('#estado').select2("val") == ""; and did job me. note actual comparison above differ bit depending on example if using multi-valued selection box.
Comments
Post a Comment