JavaScript common functions (1): jquery operation Select Basic operation

Source: Internet
Author: User

$ (this). Children (' option:selected '). Val ();//This is the value of selected.$ ("#charCity"). empty ();//Content Empty:

jquery Gets the text and value selected by select:

Copy CodeThe code is as follows:
$ ("#select_id"). Change (function () {//code ...}); Add an event for Select, which is triggered when one of the items is selected
var checktext=$ ("#select_id"). Find ("option:selected"). Text (); Gets the text selected by the Select
var checkvalue=$ ("#select_id"). Val (); Gets the value selected by select
var checkindex=$ ("#select_id"). Get (0). SelectedIndex; Gets the index value of the Select selection
var maxindex=$ ("#select_id option:last"). attr ("index"); Gets the maximum index value of the Select


jquery Add/Remove option for select:

Copy CodeThe code is as follows:
$ ("#select_id"). Append ("<option value= ' value ' >Text</option>"); Append an option to the Select (drop-down item)
$ ("#select_id"). Prepend ("<option value= ' 0 ' > Please select </option>"); Insert an option for select (first position)
$ ("#select_id option:last"). Remove (); Delete index value in select maximum option (last)
$ ("#select_id option[index= ' 0 ']"). Remove (); Delete option with index value 0 in select (first)
$ ("#select_id option[value= ' 3 ']"). Remove (); Delete option for value= ' 3 ' in select
$ ("#select_id option[text= ' 4 ']"). Remove (); Delete option for text= ' 4 ' in select


Content empty:

Copy CodeThe code is as follows:
$ ("#charCity"). empty ();

JavaScript common function (1): jquery operation Select Basic Operation

Related Article

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.