2009-12-04 17 views

Respuesta

10
var arr = new Array; 

    $("#selectboxid option").each (function() { 
     arr.push ($(this).val()); 
    }); 

alert (arr.join(',')); 

en el botón haga clic en

$("#btn1").click (function() { 
     var arr = new Array; 
     $("#selectboxid option").each (function() { 
      arr.push ($(this).val()); 
     }); 
     alert (arr); 
    }); 
3

err ok ..

$('#selectbox').click(function() { 
    var allvals = []; 
    $(this).find('option').each(function() { allvals.push($(this).val()); }; 
}); 

o tal vez quiere decir

$('#thebutton').click(function() { 
    var allvals = []; 
    $('#theselectbox').find('option').each(function() { allvals.push($(this).val()); }; 
}); 
13

Creo que es una buena oportunidad de utilizar la Traversing/map método:

var valuesArray = $("#selectId option").map(function(){ 
    return this.value; 
}).get(); 

Y si usted desea conseguir dos matrices independientes que contienen los valores seleccionados y no seleccionados se puede hacer algo como esto:

var values = { 
    selected: [], 
    unselected:[] 
}; 

$("#selectId option").each(function(){ 
    values[this.selected ? 'selected' : 'unselected'].push(this.value); 
}); 

Después de eso, los values.selected y values.unselected matrices contendrán los elementos adecuados.

Cuestiones relacionadas