The following common codes may be helpful to you:
//1. Get the selected option value
$('#selectList').val();
//2. Get the text of the selected option
$('#selectList :selected').text( );
//3. Get 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 option
switch ($('#selectList :selected').text()) {
case 'First Option':
//do something
break;
case 'Something Else':
// do something else
break;
}
// 5. Delete an option with value=2
$("#selectList option[value='2']").remove();
//6. Move the 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');
}) ;
$('#remove').click(function() {
return !$('#select2 option:selected').appendTo('#select1');
});
});
If you don’t know JQuery, you can read its documentation first.
I hope this POST is helpful to you.
Statement:The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn