jquery Operation Select Radio Box Instance code

Source: Internet
Author: User

Select box Action

Get value

The code is as follows Copy Code

$ (' #select '). Val ()

Set the item with the value XX to be selected

The code is as follows Copy Code

$ (' #select '). Val (xx)

Sets the text message to YY for the selected item

The code is as follows Copy Code

$ (' #select option[text= ' yy] '). attr ("selected", true);

Gets the index selected by select:

The code is as follows Copy Code

$ ("#ddlRegType"). Get (0). SelectedIndex;

Set Select:

Set the index selected by select:

The code is as follows Copy Code

$ ("#ddlRegType"). Get (0). Selectedindex=index;//index is the index value

Set the value selected by select:

The code is as follows Copy Code

$ ("#ddlRegType"). attr ("value", "Normal");

$ ("#ddlRegType"). Val ("Normal");

$ ("#ddlRegType"). Get (0). value = value;

Set selected text for select:

The code is as follows Copy Code

var count=$ ("#ddlRegType option"). Length;

for (Var i=0;i<count;i++)
{if ($ ("#ddlRegType"). Get (0). Options[i].text = text)
{
$ ("#ddlRegType"). Get (0). options[i].selected = true;

Break
}
}

$ ("#select_id option[text= ' jQuery ']"). attr ("selected", true);

To set the SELECT option item:

The code is as follows Copy Code

$ ("#select_id"). Append ("<option value= ' value ' >Text</option>"); Add an option

$ ("#select_id"). Prepend ("<option value= ' 0 ' > Please select </option>"); Insert an option before

$ ("#select_id option:last"). Remove (); Delete the maximum index value option

$ ("#select_id option[index= ']"). Remove ()//delete option with index value of 0

$ ("#select_id option[value= ' 3 ']"). Remove (); Delete option with a value of 3

$ ("#select_id option[text= ' 4 ']"). Remove (); Delete option with text value of 4


Radio Box operation

Get value

The code is as follows Copy Code

$ (' input[name= ' xx ']:checked '). Val ()

Set the value to XX is selected

The code is as follows Copy Code

$ (' input[name= "xx"]:radio[value= "yy"] "). attr (' checked ', ' checked ');

Gets the value of a set of radio selected items

The code is as follows Copy Code

var item = $ (' input[@name =items][@checked] '). Val ();

$ ("input[@type =radio][@checked]"). Val ();


2 setting. Radio Single Selection Group

1.$ (' input[@name =items] '). Get (1). checked = true; The second element is the currently selected value
2. $ ("input[@type =radio][@value =2]"). attr ("Checked", true);//Set value=2 item as current check

Let's look at some common examples


HTML code for the dropdown box

The code is as follows Copy Code

<select id=select> <option selected> China province </OPTION></SELECT>

Note that the ID of the select here is "select"

Add "Jiangsu" to the last one in the dropdown box
JavaScript code

The code is as follows Copy Code
$ (' #add_to_last '). Click (function () {$ (' #select '). Append (' <option value= "Jiangsu" ></OPTION> Jiangsu);});

Add "Anhui" to the first place in the dropdown box

JavaScript code

The code is as follows Copy Code

$ (' #add_to_first '). Click (function () {$ (' #select '). Prepend (' <option value= "Anhui" ></OPTION> Anhui);});

Gets the current Selectindex (the index of the item for the currently selected Drop-down menu)
JavaScript code

The code is as follows Copy Code
$ (' #get_select_index '). Click (function () {Alert ($ (' #select option:selected '). attr ("index"));

Remove Drop-down Menu Last item
JavaScript code

The code is as follows Copy Code

$ (' #remove_last_option '). Click (function () {$ (' #select option:last '). Remove ();});

Remove all options except the first one

The code is as follows Copy Code

$ (' #remove_all_option_except_first '). Click (function () {$ (' #select option '). Not (': a '). Remove ();});

Get the Drop-down menu maximum index value

  code is as follows copy code

$ (' #get_ Max_index '). Click (function () {     var maxindex=$ ("#select option:last"). attr ("index");      alert (Maxindex); });

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.