jquery Select Fetch value, assignment operation

Source: Internet
Author: User

Select >jquery Select Value, assignment operation

First, get Select

Gets the text selected by select:

$ ("#ddlRegType"). Find ("option:selected"). Text ();

Gets the index selected by select:

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

Second, set Select

Set the index selected by select:

$ ("#ddlRegType"). Get (0). SelectedIndex = Index;//index is an indexed value

Set the value selected by select:

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

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

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

Set selected text for select:

1 var count = $ ("#ddlRegType option"). Length;
2
3 for (Var i=0;i<count;i++)
4 {

5 if ($ ("#ddlRegType"). Get (0). Options[i].text = text)
6 {
7 $ ("#ddlRegType"). Get (0). options[i].selected = true;
8 break;
9}
10}

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

To set the SELECT option item:

$ ("#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

Empty Select:

$ ("#ddlRegType"). empty ();


Drop-down Box:

var cc1 = $ (". FORMC select[@name = ' country '] option[@selected]"). Text (); Gets the text of the selected item in the Drop-down menu (note that there are spaces in the middle)
var CC2 = $ ('. FORMC select[@name = ' country '] '). Val (); Gets the value of the selected item in the Drop-down menu
var cc3 = $ ('. FORMC select[@name = ' country '] '). attr ("id"); Gets the id attribute value of the selected item in the Drop-down menu
$ ("#select"). Empty ()//Empty Drop-down box//$ ("#select"). HTML (");
$ ("<option value= ' 1 ' >1111</option>"). Appendto ("#select")//option to add a drop-down box

A little explanation:
1.select[@name = ' country '] option[@selected] Represents the Name property,
and the attribute value is the option element with the selected attribute inside the Select element of ' country ';
It can be seen that a @ begins with a property that follows.

2, Radio Box:
$ ("input[@type =radio][@checked]"). Val (); Gets the value of the selected item in the Radio box (note that there are no spaces in the middle)
$ ("input[@type =radio][@value =2]"). attr ("Checked", ' checked '); Sets the value=2 of the radio box to the selected state. (Note that there are no spaces in between)

3, check box:
$ ("input[@type =checkbox][@checked]"). Val (); Gets the value of the first item selected in the check box
$ ("input[@type =checkbox][@checked]"). each (function () {//, because the check box is typically selected for more than one, you can cycle the output
Alert ($ (this). Val ());
});

$ ("#chk1"). attr ("Checked", "");/no tick.
$ ("#chk2"). attr ("Checked", true);/tick
if ($ ("#chk1"). attr (' checked ') ==undefined) {}//judge whether it has been ticked

Traversing option and adding, removing option
function Changeshipmethod (shipping) {
var len = $ ("select[@name =ishiptype] option"). length
if (shipping.value!= "CA") {
$ ("select[@name =ishiptype] option"). each (function () {
if ($ (this). val () = 111) {
$ (this). Remove ();
}
});
}else{
$ ("<option value= ' >ups ground</option>"). Appendto ($ ("select[@name =ishiptype]");
}
}


Get the selected value of the dropdown menu

$ (#testSelect option:selected '). Text ();
or $ ("#testSelect"). Find (' option:selected '). Text ();
or $ ("#testSelect"). Val ();

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.