Introduction to jquery-$.each array action and form action code

Source: Internet
Author: User

Assignment actions for radio, checkbox, select:
The code is as follows:

$ ("input[name=a]"). Val (["Entertainment 1"]);
$ ("Input[type=checkbox]"). Val (["Basketball", "game"]);
$ ("select"). Val (["Basketball", "game"]);

Code Analysis:
For radio assignment, the attribute selector obtains the radio;
Assign a value to a CheckBox, the property selector gets the checkbox, and the value is enclosed in brackets [], separated by commas in the middle of multiple values;
Assigns a value to a select, gets a select through the tag Selector,
For radio, checkbox, select Value Operations:
The code is as follows:

var checkvalue = "";
var s = $ ("input[name=a]:checked"). Val ();
$ (": checkbox:checked"). each (function () {
CheckValue + = $ (this). Val ();
});
var selectvalue = "";
$ ("select:selected"). each (function () {
Selectvalue + = $ (this). Val ();
});
Alert ("CheckValue:" + CheckValue + "Radiovalue:" + S + "Selectvalue:" + selectvalue);

Array operations

The first is the normal array (an array indexed as an integer):
$.map (ARR,FN);
Each element in the array calls the FN function to process one at a, and the FN function returns the last resulting array.
The code is as follows:

var arr = [9, 8, 7, 6, 5, 4, 3, 2, 1];
var newarr = $.map (arr, function (item) {return item*2});
alert (newarr);

$.each (ARRAY,FN) array of arrays each element calls the FN function for processing, no return value
The code is as follows:

var arr = [9, 8, 7, 6, 5, 4, 3, 2, 1];
$.each (arr, function (key, value) {Alert ("key:" + key + "value:" + value);});

You can also omit the parameter of a function, this time this can get the value of the current element being traversed
The code is as follows:

var arr = [9, 8, 7, 6, 5, 4, 3, 2, 1];
$.each (arr, function () {alert (this);});

Then there is the array of key values indexed as strings, and for such arrays,
Generally used $.each (ARRAY,FN) to operate:
The code is as follows:

var arr = {"Jim": "One", "Tom": "One", "Lilei": "13"};
$.each (arr, function (key, value) {alert ("Name:" +key+ "Age:" +value);});

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.