jquery Operations Select Dropdown box multiple methods (check, value, assignment, etc.)

Source: Internet
Author: User

jquery Gets the text and value selected by select:
Syntax Explanation:
1. $ ("#select_id"). Change (function () {//code ...});Add an event for Select, which is triggered when one of the items is selected
2. Var checktext=$ ("#select_id"). Find ("option:selected"). Text (); //Gets the text selected by select
3. Var checkvalue=$ ("#select_id"). Val (); //Gets the value selected by select
4. Var checkindex=$ ("#select_id"). Get (0). SelectedIndex; //Gets the index value of select selection
5. Var maxindex=$ ("#select_id option:last"). attr ("index"); //Get the Select Maximum index value
jquery sets the text and value selected for select:
Syntax Explanation:
1. $ ("#select_id"). Get (0). selectedindex=1; //Set the Select index value to 1 to select
2. $ ("#select_id"). Val (4); //Set Select with a value of 4 for Select
3. $ ("#select_id option[text= ' jQuery ']"). attr ("selected", true); //Set Select the text value for the jquery item selected

jquery Add/Remove option for select:
Syntax Explanation:
1. $ ("#select_id"). Append ("<option value= ' value ' >Text</option>");//Append an option to the Select (drop-down item)
2. $ ("#select_id"). Prepend ("<option value= ' 0 ' > Please select </option>"); //Insert an option for select (first position)
3. $ ("#select_id option:last"). Remove (); //Delete index value in select maximum option (last)
4. $ ("#select_id option[index= ' 0 ']"). Remove (); //delete option with index value 0 in select (first)
5. $ ("#select_id option[value= ' 3 ']"). Remove (); //delete option for value= ' 3 ' in select
5. $ ("#select_id option[text= ' 4 ']"). Remove (); //delete option for text= ' 4 ' in select Http://www.k686.com

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

----------------------------------------------------------------------------------------------------

Traverse option and Add, remove 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= ' 111 ' >ups ground</option>"). AppendTo ($ ("select[@name =ishiptype]"));
}
}


Get the selected value of the dropdown

$ (#testSelect option:selected '). Text ();
or $ ("#testSelect"). Find (' option:selected '). Text ();
or $ ("#testSelect"). Val ();
//////////////////////////////////////////////////////////////////
A bad memory can be collected:
1, 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 (); Get 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 the 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] means having the Name property,
and the option element with the selected attribute in the Select element with the attribute value of ' country ';
You can see that there is an @ at the beginning of the property is followed by.

2, Radio Box:
$ ("input[@type =radio][@checked]"). Val (); Get 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 there is no space in the middle)

3, check box:
$ ("input[@type =checkbox][@checked]"). Val (); Gets the value of the check box for the first item selected
$ ("input[@type =checkbox][@checked]"). each (function () {////Because the check box is generally selected as multiple, you can loop the output
Alert (This). Val ());
});

$ ("#chk1"). attr ("Checked", "");//Not ticked
$ ("#chk2"). attr ("Checked", true);//tick
if ($ ("#chk1"). attr (' checked ') ==undefined) {}//Determine if a tick has been made


Of course the jquery selector is powerful. There are many other ways.

<script src= "Jquery-1.2.1.js" type= "Text/javascript" ></script>
<script language= "javascript" type= "Text/javascript" >
$ (document). Ready (function () {
$ ("#selectTest"). Change (function ()
{
Alert ("Hello");
Alert ($ ("#selectTest"). attr ("name"));
$ ("a"). attr ("href", "xx.html");
Window.location.href= "xx.html";
Alert ($ ("#selectTest"). Val ());
Alert ($ ("#selectTest option[@selected]"). text ());
$ ("#selectTest"). attr ("Value", "2");

});
});
</script>


<a href= "#" >aaass</a>

<!--drop-down box--
<select id= "Selecttest" name= "Selecttest" >
<option value= "1" >11</option>
<option value= "2" >22</option>
<option value= "3" >33</option>
<option value= "4" >44</option>
<option value= "5" >55</option>
<option value= "6" >66</option>
</select>
jquery Radio value, checkbox value, select Value, Radio selected, CheckBox selected, select selected, and its related to get a set of radio values of the selected item
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
$ ("<optionvalue= ' 1 ' & gt;1111</option><optionvalue= ' 2 ' >2222</option& gt;"). AppendTo ("#sel")//option to add a drop-down box
$ ("#sel"). empty ();//Empty drop-down box

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

Reprinted too many times, the original author has not been tested, thanks to the original author's contribution, as well as k686 Green Software (http://www.k686.com/) feeds.

jquery Operations Select Dropdown box multiple methods (check, value, assignment, etc.)

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.