// 1. Obtain the selected option value
$ ('# SelectList'). val ();
// 2. Obtain the text of the selected option
$ ('# SelectList: selected'). text ();
// 3. Obtain multiple selected option values and texts
Var foo = [];
$ ('# Multiple: selected'). each (function (I, selected ){
Foo [I] = $ (selected). text ();
});
// To get the selected values, just use. val ()-this returns a string or array
Foo = $ ('# multiple: selected'). val ();
// 4. Use the conditional expression of option
Switch ($ ('# selectList: selected'). text ()){
Case 'first Option ':
// Do something
Break;
Case 'something else ':
// Do something else
Break;
}
// 5. Delete the option with a value = 2
$ ("# SelectList option [value = '2']"). remove ();
// 6. Move option from list A to list B.
// Here we have 2 select lists and 2 buttons. If you click the "add" button,
// We remove the selected option from select1 and add that same option to select2.
// The "remove" button just does things the opposite way around.
// Thanks to jQuery's chaining capabilities, what was once a rather tricky undertaking with JS can now be done in 6 lines of code.
$ (). Ready (function (){
$ ('# Add'). click (function (){
Return! $ ('# Select1 option: selected'). appendTo (' # select2 ');
});
$ ('# Delete'). click (function (){
Return! $ ('# Select2 option: selected'). appendTo (' # select1 ');
});
});