Example of jQuery getting select selected text and values _ jquery-js tutorial

Source: Internet
Author: User
The text and values selected by the select statement are useful in many places and there are many implementation methods, in the following example, we will introduce in detail how jquery obtains the select statement:
Obtain the selected text in the select statement:
$ ("# Ddlregtype"). find ("option: selected"). text ();

Obtain the value selected by the select statement:
$ ("# Ddlregtype"). val ();

Obtain the index selected by the select statement:
$ ("# Ddlregtype"). get (0). selectedindex;

Set select:
Set the index selected by select:
$ ("# Ddlregtype"). get (0). selectedindex = index; // index indicates the index value.

Set the value selected by select:

The Code is as follows:


$ ("# Ddlregtype"). attr ("value", "normal");
$ ("# Ddlregtype"). val ("normal ");
$ ("# Ddlregtype"). get (0). value = value;


Set select selected text:

The Code is as follows:


Var count = $ ("# ddlregtype option"). length;
For (var I = 0; 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 );


Set select option items:

The Code is as follows:


$ ("# Select_id"). append ("Text"); // Add an option
$ ("# Select_id"). prepend ("Select"); // Insert an option
$ ("# Select_id option: last"). remove (); // Delete the option with the largest index value
$ ("# Select_id option [index = '0']"). remove (); // delete an option with an index value of 0
$ ("# Select_id option [value = '3']"). remove (); // delete an option with a value of 3
$ ("# Select_id option [text = '4']"). remove (); // delete an option with a text value of 4


Clear select:

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

To work, you need to obtain the values in the two forms.

How do I obtain the value added from the left-side selection box to the right-side selection box? I thought about how to use webpage special effects. Here I use jquery, which is quite popular.
The js Code is as follows:

The Code is as follows:


// Obtain all attribute values var item =$ ("# select1"). val ();
$ (Function (){
$ ('# Select1'). each (// obtain all values of select1
Function (){
$ ('Button '). click (function (){
Alert ($ ('# select2'). val (); // obtain the select1 value in select2
});
});
})
Script


It is worth noting that it cannot be written directly.

The Code is as follows:


$ (Function (){
$ ('# Select2'). each (// obtain all values of select1, because the options are added from the left to the right, jquery does not actually pass the value from the left to the right.
Function (){
$ ('Button '). click (function (){
Alert ($ (this). val (); // obtain the select1 value in select2
});
});
})


Html:

The Code is as follows:



Option 1 Option 2 Option 3 Option 4 Option 5 Option 6 Option 7


Select Add to right>
Add all to the right>






< <选中删除到左边
< <全部删除到左边




Use JQuery and Ajax to dynamically fill the Select option options

The Code is as follows:


// Bind ClassLevel1 Click Event
$ ("# ClassLevel1"). change (function (){
Var id = $ ("# ClassLevel1"). val ();
Var level2 = $ ("# ClassLevel2 ");
Level2.empty ();
$ ("# ClassLevel3"). hide ();
$. Ajax ({
Url: "./askCommon. ashx? Action = getclasslevel & pid = "+ id,
Data: {"type": "ajax "},
Datatype: "json ",
Type: "get ",
Success: function (data ){
Var json = eval_r (data );
For (var ind in json ){
Level2.append ($ (""+ Json [ind]. typename +""));
}

}
});
})

Related Article

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.