Turn: jquery obtains the select option value and the operation on the select option

Source: Internet
Author: User

Obtain select:

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:

$ ("# Ddlregtype"). ATTR ("value", "normal");

$ ("# Ddlregtype"). Val ("normal ");

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

 

Set select selected text:

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 );

 

Set select option items:

 

$ ("# Select_id"). append ("<option value = 'value'> text </option>"); // Add an option

$ ("# Select_id"). prepend ("<option value = '0'> select </option>"); // 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 ();

 

Jquery obtains the following values:

. Val ()

. Text ()

 

Set Value

. Val ('set the value here ')

 

 

$ ("document "). ready (function () {
$ ("# btn1 "). click (function () {
$ ("[name = 'checkbox']"). ATTR ("checked", 'true'); // select all
})
$ ("# btn2 "). click (function () {
$ ("[name = 'checkbox']"). removeattr ("checked"); // cancel all selections
})
$ ("# btn3 "). click (function () {
$ ("[name = 'checkbox']: Even "). ATTR ("checked", 'true'); // select all odd numbers
})
$ ("# btn4 "). click (function () {
$ ("[name = 'checkbox']"). each (function () {// reselect
if ($ (this ). ATTR ("checked") {
$ (this ). removeattr ("checked");
}< br> else {
$ (this ). ATTR ("checked", 'true');
}< BR >})
$ ("# btn5 "). click (function () {// output the 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.