jquery operation Select (Add, delete, empty)

Source: Internet
Author: User

jQuery gets the Text and Value selected by select:

123456789 $("#select_id").change(function(){//code...}); //为Select添加事件,当选择其中一项时触发 varcheckText=$("#select_id").find("option:selected").text(); //获取Select选择的textvarcheckValue=$("#select_id").val(); //获取Select选择的ValuevarcheckIndex=$("#select_id ").get(0).selectedIndex; //获取Select选择的索引值varmaxIndex=$("#select_id option:last").attr("index"); //获取Select最大的索引值

jquery Add/Remove option for select:

1234567891011 $("#select_id").append("<option value=‘Value‘>Text</option>"); //为Select追加一个Option(下拉项)$("#select_id").prepend("<option value=‘0‘>请选择</option>"); //为Select插入一个Option(第一个位置)$("#select_id option:last").remove(); //删除Select中索引值最大Option(最后一个) $("#select_id option[index=‘0‘]").remove(); //删除Select中索引值为0的Option(第一个)$("#select_id option[value=‘3‘]").remove(); //删除Select中Value=‘3‘的Optiona$("#select_id option[text=‘4‘]").remove(); //删除Select中Text=‘4‘的Optiona

Content empty:

1 $("#charCity").empty();

http://blog.csdn.net/nairuohe/article/details/6307367

Every time the operation of the Select, always to come out to turn over the information, as a summary of their own, after the turn over here.

such as <select class= "selector" ></select>

1. Set value to Pxx item selected

     $ (". Selector"). Val ("Pxx");

2. Set text to Pxx to select

    $ (". Selector"). Find ("option[text= ' Pxx ')"). attr ("selected", true);

Here is the use of a bracket, preceded by the attribute name in parentheses, without quotation marks. In many cases, the use of brackets can make logic very simple.

3. Gets the value of the currently selected item

    $ (". Selector"). Val ();

4. Gets the text of the currently selected item

    $ (". Selector"). Find ("option:selected"). Text ();

Using a colon here, mastering its usage and extrapolate will also make the code concise.

There are many times when a select cascade is used, that is, the value of the second select changes with the value selected in the first select. This is very simple in jquery.

Such as:

$ (". Selector1"). Change (function () {     //Empty the second      $ (". Selector2") first. empty ();     In real-world applications, the option here is typically generated by looping multiple      var option = $ ("<option>"). Val (1). Text ("Pxx");      $ (". Selector2"). Append (option);});

Http://www.cnblogs.com/yaoshiyou/archive/2010/08/24/1806939.html

jquery Gets the text and value selected by select:
Syntax Explanation:

$ ("#select_id"). Change (function () {//code ...});   Adds an event for select, triggering var checktext=$ ("#select_id") when one of the items is selected. Find ("option:selected"). Text ();  Gets the Textvar checkvalue=$ ("#select_id") selected by the Select. Val ();  Gets the Valuevar checkindex=$ ("#select_id") selected by the Select. Get (0). SelectedIndex;  Gets the index value of select selected Var maxindex=$ ("#select_id option:last"). attr ("index");  Gets the maximum index value of the Select


jquery sets the text and value selected for select:
Syntax Explanation:

$ ("#select_id"). Get (0). selectedindex=1;  Set the Select index value to 1 to select $ ("#select_id"). Val (4);   Set the value of the select value to 4 to select $ ("#select_id option[text= ' jQuery ']"). attr ("selected", true);   Set the text value of select to the item selected for jquery

jquery Add/Remove option for select:
Syntax Explanation:

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

Http://www.cnblogs.com/SAL2928/archive/2008/10/28/1321285.html

jquery Radio value, checkbox value, select Value, Radio selected, CheckBox selected, select selected, and its related
Gets the value of a set of radio selected items
var item = $ (' input[name=items][checked] '). Val ();
Gets the text of the Select selected item
var item = $ ("Select[name=items] option[selected]"). Text ();
The second element of the Select drop-down box is the currently selected value
$ (' #select_id ') [0].selectedindex = 1;
Radio the second element of a radio group is the currently selected value
$ (' input[name=items] '). Get (1). checked = true;
Get Value:
text box, text area: $ ("#txt"). attr ("value");
Multi Box checkbox:$ ("#checkbox_id"). attr ("value");
Radio Group Radio: $ ("input[type=radio][checked]"). Val ();
Drop-down box select: $ (' #sel '). Val ();
Control form elements:
text box, text area: $ ("#txt"). attr ("Value", "");//Clear Contents
$ ("#txt"). attr ("value", ' 11 ');//fill content
Multiple marquee checkbox: $ ("#chk1"). attr ("Checked", "");//No Tick
$ ("#chk2"). attr ("Checked", true);//tick
if ($ ("#chk1"). attr (' checked ') ==undefined)//Determine if a tick has been made
Radio Group Radio: $ ("Input[type=radio]"). attr ("Checked", ' 2 ');//Set value=2 for the currently selected item
Drop-down box select: $ ("#sel"). attr ("value", '-sel3 ');//Set the VALUE=-SEL3 item as the current selection
$ ("<option value= ' 1 ' >1111</option><option value= ' 2 ' >2222</option>"). AppendTo ("#sel")// option to add a drop-down box
$ ("#sel"). empty ();//Empty drop-down box

jquery operation Select (Add, delete, empty)

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.