jquery Gets the value of option and operates on option

Source: Internet
Author: User
jquery Gets the value of option and operates on option


This article introduces jquery to get the value of option and the operation of option, below is a good example analysis and sample code, interested friends can understand the next

jquery Gets the Select element and selects the text and value:
Copy the code code as follows:
$ ("#select_id"). Change (function () {//code ...}); Add an event for Select, which is triggered when one of the items is selected
var checktext=$ ("#select_id"). Find ("option:selected"). Text (); Gets the text selected by the Select
var checkvalue=$ ("#select_id"). Val (); Gets the value selected by select
var checkindex=$ ("#select_id"). Get (0). SelectedIndex; Gets the index value of the Select selection
var maxindex=$ ("#select_id option:last"). attr ("index"); Gets the maximum index value of the Select

jquery Gets the Select element and sets the text and value:

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

jquery Add/Remove option item for SELECT element:

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

Level three classification <select name= "Thirdlevel" id= "Thirdlevel"
Onchange= "Getfourthlevel ()" >
<option value= "0" id= "thirdoption" >
Please select level Three category
</option>
</select>
</div>

Level Four classification:
<select name= "Fourthlevelid" id= "Fourthlevelid" >
<option value= "0" id= "fourthoption" >
Please select level Four category
</option>
</select>
</div>
if ($ ("#thirdLevel"). Val ()!=0) {
$ ("#thirdLevel option[value!=0]"). Remove ();
}
if ($ ("#fourthLevelId"). Val ()!=0) {
$ ("#fourthLevelId option[value!=0]"). Remove ();
}//This expression: if we want to select the third class when choosing: if there is data in class fourth, delete if there is no data in class Fourth of the commodity as the default value. It is often used after learning Ajax techniques later.

Get select:
Gets the text selected by the Select:
$ ("#ddlRegType"). Find ("option:selected"). Text ();
Gets the value selected by select:

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

Gets the index selected by the select:
$ ("#ddlRegType"). Get (0). SelectedIndex;
Set Select:
Set the index selected by select:
$ ("#ddlRegType"). Get (0). Selectedindex=index;//index as index value

Set the value selected by select:
Copy the code code as follows:
$ ("#ddlRegType"). attr ("value", "Normal");
$ ("#ddlRegType"). Val ("Normal");
$ ("#ddlRegType"). Get (0). value = value;

Set the text selected by select:
Copy the code code as follows:
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 entry:
Copy the code code as follows:
$ ("#select_id"). Append ("<option value= ' value ' >Text</option>"); Add an option
$ ("#select_id"). Prepend ("<option value= ' 0 ' > Please select </option>"); Insert an option in front
$ ("#select_id option:last"). Remove (); Remove the most indexed value option
$ ("#select_id option[index= ' 0 ']"). Remove ();//delete option with index value 0
$ ("#select_id option[value= ' 3 ']"). Remove (); Delete option with a value of 3
$ ("#select_id option[text= ' 4 ']"). Remove (); Delete option with a text value of 4

Clear Select:

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

jquery Gets the value:

Val ()
Text ()

Setting the value
Val (' Set value here ')
Copy the code code as follows:
$ ("document"). Ready (function () {
$ ("#btn1"). Click (function () {
$ ("[name= ' checkbox ']"). attr ("Checked", ' true ');//Select All
})
$ ("#btn2"). Click (function () {
$ ("[name= ' checkbox ']"). Removeattr ("checked");//Cancel Select all
})
$ ("#btn3"). Click (function () {
$ ("[name= ' checkbox ']:even"). attr ("Checked", ' true ');//Select All odd
})
$ ("#btn4"). Click (function () {
$ ("[name= ' checkbox ']"). each (function () {//Counter-Select
if ($ (this). attr ("checked")) {
$ (this). Removeattr ("checked");
}
else{
$ (this). attr ("Checked", ' true ');
}
})
})
$ ("#btn5"). Click (function () {//Output selected value
var str= "";
$ ("[name= ' checkbox '][checked]"). each (function () {
str+=$ (This). Val () + "\ r \ n";
Alert (This). Val ());
})
alert (str);
})
})

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.