Jquery judges whether radio, selelct, and checkbox are selected and obtains the selected value.

Source: Internet
Author: User
This article mainly introduces Jquery's method to determine whether radio, selelct, and checkbox are selected and to obtain the selected value. This article summarizes the method in a concise and straightforward manner, for more information, see Jquery returns the value of the radio button.

The code is as follows:


$ ("Input [name = 'items ']: checked"). val ();


In addition, determine whether radio is selected and obtain the selected value.

As follows:

The code is as follows:


Function checkradio (){
Var item = $ (": radio: checked ");
Var len = item. length;
If (len> 0 ){
Alert ("yes -- the selected value is:" + $ (": radio: checked"). val ());
}
}



Jquery radio, checkbox, select, radio, checkbox, select, and related

Obtains the value of a set of radio selected items.

The code is as follows:


Var item = $ ('input [name = items] [checked] '). val ();


Obtain the text of the selected select item.

The code is as follows:


Var item = $ ("select [name = items] option [selected]"). text ();


The second element in the select drop-down box is the selected value.

The code is as follows:


$ ('# Select_id') [0]. selectedIndex = 1;


The second element of the radio Group is the currently selected value.

The code is as follows:


$ ('Input [name = items] '). get (1). checked = true;



Get value:

Text box, text area: $ ("# txt"). attr ("value ");
Multiple choice box checkbox: $ ("# checkbox_id"). attr ("value ");
Radio Group radio: $ ("input [type = radio] [checked]"). val ();
Drop-down box select: $ ('# sel'). val ();

Control form elements:

Text box, text area: $ ("# txt"). attr ("value", ''); // clear the content

The code is as follows:


$ ("# Txt"). attr ("value", '11'); // fill in the content



Multiple choice box checkbox: $ ("# chk1"). attr ("checked", ''); // do not check

The code is as follows:


$ ("# Chk2"). attr ("checked", true); // Check
If ($ ("# chk1"). attr ('checked') = undefined) // You can check whether the checked has been checked.

Radio Group radio: $ ("input [type = radio]"). attr ("checked", '2'); // set the project with value = 2 as the currently selected item

Drop-down box select: $ ("# sel"). attr ("value", '-sel3'); // set the value =-sel3 project to the currently selected item

The code is as follows:


$ ("11112222"). AppendTo (" # sel ") // add the option in the drop-down box
$ ("# Sel"). empty (); // clear the drop-down list



I was just getting started with jquery and was unfamiliar with many things.

When $ ("# id") is used to obtain the page input element, it is found that $ ("# id"). value cannot get the value.

Finally, with the help of the great Baidu, I found the cause of the problem:

$ ("") Is a jquery object, not a dom element

Value is the attribute of dom element.

Jquery corresponds to val.

Val (): obtains the current value of the first matching element.

Val (val): sets the value of each matching element.

Therefore, the code should be written as follows:


Value: val = $ ("# id") [0]. value;
Value assignment: $ ("# id") [0]. value = "new value ";
Or $ ("# id"). val ("new value ");
Alternatively, you can: val = $ ("# id"). attr ("value ");

In jQuery, each is very useful. it is often used to replace javascript's for loop.

For example, if there is an each in a function and a condition in the each is true, the function returns true or false.

The code is as follows:


Function methodone (){
....
$. Each (array, function (){
If (condition true ){
Return true;
}
});
....
}


It turns out that it is always incorrect.

Later, I found that break and continue cannot be used in the each code block. to implement the break and continue functions, I should use other methods.
Break ---- return false;
Continue -- use return ture;

Therefore, when I want to use return true in each to return the function to this function, I only want the each to continue to execute
The each is not interrupted, so the function cannot return.

In addition, determine whether radio is selected and obtain the selected value.

As follows:

The code is as follows:


Function checkradio (){
Var item = $ (": radio: checked ");
Var len = item. length;
If (len> 0 ){
Alert ("yes -- the selected value is:" + $ (": radio: checked"). val ());
}
}

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.