JQuery select tag operation code segment

Source: Internet
Author: User

The following code may be helpful to you:
Copy codeThe Code is as follows:
// 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 ');
});
});

If you do not know JQuery, you can read its documentation first.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.