jquery drop-down box

Source: Internet
Author: User

Source: https://www.cnblogs.com/songhaipeng/archive/2012/11/25/2787214.html

This article is reproduced and comes

jquery on the dropdown box select operation Summary

Turn from the network, leave it as a backup

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

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 the 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 the Select selection
5. 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:
1. $ ("#select_id"). Get (0). selectedindex=1; Set the Select index value to 1 to select
2. $ ("#select_id"). Val (4); Sets the value of select to 4 for the item selected
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 item for SELECT element:
Example Analysis:
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
6. $ ("#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. After learning Ajax technology in the back, often use!

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:

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

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

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

Set the text selected by select:

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:

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

$ ("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);
})
})

jquery 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.