Js,jquery get the value of the Select,dropdownlist,checkbox drop-down list box (sample code) _javascript Tips

Source: Internet
Author: User

jquery Gets the text and value selected by select:
Syntax Explanation:
1. $ ("#select_id"). Change (function () {//code ...}); Adds an event for a select that fires 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 of the Select selection
4. Var checkindex=$ ("#select_id"). Get (0). SelectedIndex; Get the index value selected by select
5. Var maxindex=$ ("#select_id option:last"). attr ("index"); Get the maximum index value for select

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

jquery Add/Remove option items for select:
Syntax Explanation:
1. $ ("#select_id"). Append ("<option value= ' value ' >Text</option>"); Append an option to select (dropdown)
2. $ ("#select_id"). Prepend ("<option value= ' 0 ' > Please select </option>"); Inserts an option for the Select (first position)
3. $ ("#select_id option:last"). Remove (); Delete index value maximum option (last) in select
4. $ ("#select_id option[index= ' 0 ']"). Remove (); Delete option with index value of 0 in select (first)
5. $ ("#select_id option[value= ' 3 ']"). Remove (); Delete option value= ' 3 ' in select
5. $ ("#select_id option[text= ' 4 ']"). Remove (); Delete option text= ' 4 ' in select

jquery Radio value, checkbox value, select Value, Radio selected, CheckBox selected, select selected, and 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");
Multiple-selection box checkbox:$ ("#checkbox_id"). attr ("value");
Radio Group Radio: $ ("input[@type =radio][@checked]"). Val ();
Dropdown box Select: $ (' #sel '). Val ();

Control form elements:
text box, text area: $ ("#txt"). attr ("Value", "");/empty content
$ ("#txt"). attr ("value", ' 11 ');//fill content

Multi-Marquee checkbox: $ ("#chk1"). attr ("Checked", "");/no tick.
$ ("#chk2"). attr ("Checked", true);/tick
if ($ ("#chk1"). attr (' checked ') ==undefined)//judge whether it has been ticked

Single-select group radio: $ ("input[@type =radio]"). attr ("Checked", ' 2 ');//Set value=2 project is currently selected
Dropdown box Select: $ ("#sel"). attr ("value", '-sel3 ');//Set VALUE=-SEL3 item as 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

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

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 Select value of the Drop-down menu

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

a bad memory can be collected:
1, dropdown 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

Of course jquery's 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>

<!--dropdown 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 related to get a set of radio selected values
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");
Multiple-selection box checkbox:$ ("#checkbox_id"). attr ("value");
Radio Group Radio: $ ("input[@type =radio][@checked]"). Val ();
Dropdown box Select: $ (' #sel '). Val ();

controls the form element:
text box, text area: $ ("#txt"). attr ("Value", "");/empty contents
$ ("#txt"). attr ("value", ' 11 '); Fill content
Multiple-selection box checkbox: $ ("#chk1"). attr ("Checked", "");/no tick
$ ("#chk2"). attr ("Checked", true);/tick
if ($ ("#chk1 "). attr (' checked ') ==undefined)//To determine if it has been ticked
Radio Group Radio: $ (" input[@type =radio] "). attr (" Checked ", ' 2 ');//Set value= 2 of the item is currently selected
Drop-down box select: $ ("#sel"). attr ("value", '-sel3 ');//Set VALUE=-SEL3 project is current selected
$ ("< Optionvalueoptionvalue= ' 1 ' >1111</option><optionvalueoptionvalue= ' 2 ' >2222</option> '). Appendto ("#sel")/Option
$ ("#sel") to add a Drop-down box. Empty ()//Empty Drop-down box

Get a set of radio selected values
var item = $ (' input[@name = Items][@checked]. Val ();
Get text for 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; The second element of the
Radio Radio Group is the currently selected value
$ (' input[@name =items] '). Get (1). checked = true;

Get Value:
text box, text area: $ ("#txt"). attr ("value");
Multiple-selection box checkbox:$ ("#checkbox_id"). attr ("value");
Radio Group Radio: $ ("input[@type =radio][@checked]"). Val ();
Dropdown box Select: $ (' #sel '). Val ();

Control form elements:
text box, text area: $ ("#txt"). attr ("Value", "");/empty content
$ ("#txt"). attr ("value", ' 11 ');//fill content
Multi-Marquee checkbox: $ ("#chk1"). attr ("Checked", "");/no tick.
$ ("#chk2"). attr ("Checked", true);/tick
if ($ ("#chk1"). attr (' checked ') ==undefined)//judge whether it has been ticked
Single-select group radio: $ ("input[@type =radio]"). attr ("Checked", ' 2 ');//Set value=2 project is currently selected
Dropdown box Select: $ ("#sel"). attr ("value", '-sel3 ');//Set VALUE=-SEL3 item as 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

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.