Tengo una selección con 2 grupos optg. ¿Hay alguna forma de llamar a una función solo si se selecciona una opción del primer grupo de opción y llamar a otra función si se selecciona una opción del segundo grupo de opción?Jquery - Seleccionar optgroup en seleccionar
7
A
Respuesta
26
Sure.
HTML:
What is your preferred vacation spot?<br>
<SELECT ID="my_select">
<OPTGROUP LABEL="OptGroup One." id="one">
<OPTION LABEL="Florida">Florida</OPTION>
<OPTION LABEL="Hawaii">Hawaii</OPTION>
<OPTION LABEL="Jersey">Jersey Shore</OPTION>
</OPTGROUP>
<OPTGROUP LABEL="OptGroup Two" id="two">
<OPTION LABEL="Paris">Paris</OPTION>
<OPTION LABEL="London">London</OPTION>
<OPTION LABEL="Florence">Florence</OPTION>
</OPTGROUP>
</SELECT>
JS:
$("#my_select").change(function(){
var selected = $("option:selected", this);
if(selected.parent()[0].id == "one"){
//OptGroup 1, do something here..
} else if(selected.parent()[0].id == "two"){
//OptGroup 2, do something here
}
});
Ejemplo aquí: http://jsfiddle.net/pyG2v/
1
$('#selectID').change(function(){
var $option = $('option:selected', this); // get selected option
var optGroup = $option.closest('optgroup').index(); // get which optgroup
if(optGroup == 0){
// first
}
else if(optGroup == 1){
// second
}
else{
// not first or second
}
});
0
Al hacer clic en la opción que reciben el nombre de ID de grupo de opciones.
var obj = {};
$('select[name=queue]').on('change', function() {
obj = {};
var options = $('option:selected', this); //the selected options
$.each(options, function (index, option) {
var optgroupIndex = $(option).closest('optgroup').index() //get the index
var optgroupId = $(option).parent()[0].id //get id
if (obj.hasOwnProperty(optgroupId)) {
obj[optgroupId].push($(option).val());
} else {
obj[optgroupId] = [$(option).val()];
}
});
var textRows = [];
$.each(obj, function(optgroupId, values){
textRows.push(optgroupId +": " + values.join(', '));
});
$('#demo').html(textRows.join("<br>"));
});
/*Additional*/
select::-webkit-scrollbar {display:none;}
select::-moz-scrollbar {display:none;}
select::-o-scrollbar {display:none;}
select::-google-ms-scrollbar {display:none;}
select::-khtml-scrollbar {display:none;}
select{height:150px;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select name='queue' multiple>
<optgroup label="Frist Queue" id="Frist Queue">
<option value="Person1">Person1</option>
<option value="Person2">Person2</option>
<option value="Person3">Person3</option>
</optgroup>
<optgroup label="Second Queue" id="Second Queue">
<option value="Person1">Person1</option>
<option value="Person2">Person2</option>
</optgroup>
</select>
<div id="demo"></div>
Cuestiones relacionadas
- 1. índice de seleccionar la opción en un optgroup con jQuery
- 2. Seleccionar con optgroup en Symfony 2.0
- 3. Cómo habilito/deshabilito opciones en Seleccionar cuadro usando jquery
- 4. jQuery Validar obligatorio Seleccionar
- 5. jQuery Combobox/seleccionar autocompletar?
- 6. clearing seleccionar usando jquery
- 7. jquery seleccionar vivo onchange
- 8. jQuery seleccionar antecesor
- 9. jquery: seleccionar texto evento
- 10. Jquery seleccionar eventos
- 11. jQuery seleccionar según el texto
- 12. Will jQuery seleccionar elementos ocultos
- 13. jQuery autocompletar no seleccionar elemento
- 14. jquery buscar seleccionar opciones atributos
- 15. jQuery selectores: multiselección vs seleccionar
- 16. Seleccionar texto interno con jQuery
- 17. Seleccionar elemento actual de jQuery
- 18. MySQL Seleccionar desde: Seleccionar
- 19. jQuery onchange seleccionar la opción
- 20. seleccionar recuento (*) de seleccionar
- 21. Jquery Uniform cambiar seleccionar tamaño
- 22. jQuery seleccionar img con src
- 23. jQuery eliminar opciones de seleccionar
- 24. jQuery: ¿seleccionar atributo de estilo?
- 25. Adición optgroups para seleccionar dinámicamente mediante JavaScript
- 26. Usando Selenium para seleccionar una opción en una selección con optgroup
- 27. Seleccionar un elemento en iFrame jQuery
- 28. usando xpath para seleccionar elementos en jQuery?
- 29. ¿Cómo seleccionar por estilos CSS en jQuery?
- 30. seleccionar hijo más profundo en jQuery
increíble. El código parece tan claro y fácil, pero luché mucho en eso. ¡Gracias! – user495915
+1 por ser más rápido que yo. –