jquery Determines whether the radio, SELELCT, checkbox is selected and gets the selected value method summary _jquery

Source: Internet
Author: User

jquery Fetch Radio The value of the radio button

Copy Code code as follows:

$ ("Input[name= ' Items ']:checked"). Val ();

another: Determine if Radio is selected and gets the selected value

As shown below:
Copy Code code as follows:

function Checkradio () {
var item = $ (": radio:checked");
var len=item.length;
if (len>0) {
Alert ("yes--selected value is:" +$ (": radio:checked"). Val ());
}
}


jquery Radio value, checkbox value, select Value, Radio selected, CheckBox selected, select selected, and related

Gets the value of a set of radio selected items
Copy Code code as follows:

var item = $ (' input[name=items][checked] '). Val ();

gets the text of the Select selected item
Copy Code code as follows:

var item = $ ("Select[name=items] option[selected]"). Text ();

the second element of the Select Drop-down box is the currently selected value
Copy Code code as follows:

$ (' #select_id ') [0].selectedindex = 1;

Radio The second element of a radio group is the currently selected value
Copy Code code as follows:

$ (' input[name=items] '). Get (1). checked = true;


Get Value:

text box, text area: $ ("#txt"). attr ("value");
Multiple-selection box checkbox:$ ("#checkbox_id"). attr ("value");
Radio Group Radio: $ ("input[type=radio][checked]"). Val ();
Dropdown box Select: $ (' #sel '). Val ();

Control form elements:

text box, text area: $ ("#txt"). attr ("Value", "");/empty content

Copy Code code as follows:

$ ("#txt"). attr ("value", ' 11 ');//fill content


Multi-Marquee checkbox: $ ("#chk1"). attr ("Checked", "");/no tick.
Copy Code code as follows:

$ ("#chk2"). attr ("Checked", true);/tick
if ($ ("#chk1"). attr (' checked ') ==undefined)//judge whether it has been ticked

Radio Group Radio: $ ("Input[type=radio]"). attr ("Checked", ' 2 ');//Set value=2 project is currently selected

Dropdown box Select: $ ("#sel"). attr ("value", '-sel3 ');//Set VALUE=-SEL3 item as current selection

Copy Code code as follows:

$ ("<option value= ' 1 ' >1111</option><option value= ' 2 ' >2222</option>"). Appendto ("#sel")// Option to add a drop-down box
$ ("#sel"). empty ();//Empty Drop-down box


Just beginning to touch jquery, a lot of things are unfamiliar

When you use $ ("#id") to get the input element of the page, $ ("#id") is found. Value cannot be fetched

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

$ ("") is a jquery object, not a DOM element

Value is a property of DOM element

jquery corresponds to Val.

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

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

So the code should write this:


Value: val = $ ("#id") [0].value;
Assignment: $ ("#id") [0].value = "New value";
or $ ("#id"). Val ("new value");
Or this can also be: val = $ ("#id"). attr ("value");

Each of these is very useful in jquery, and is commonly used to replace JavaScript for loops

For example, in a function, there is a each, and in each case a condition is set, the function returns TRUE or FALSE

Copy Code code as follows:

function MethodOne () {
....
$.each (Array,function () {
if (condition set) {
return true;
}
});
....
}

It turns out that it's always wrong.

Later find data found that in each block of code can not use break and continue, to implement the break and continue function, to use the other way
Break----with return false;
Continue--use return ture;

So when I want to return to this function in each, I just let each continue and
There's no interruption to each, so the function can't return.

Another: Determine if Radio is selected and gets the selected value

As shown below:
Copy Code code as follows:

function Checkradio () {
var item = $ (": radio:checked");
var len=item.length;
if (len>0) {
Alert ("yes--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.