JQuery $. each traverses the JavaScript array object instance, jquery. each
View a simple jQuery example to traverse a JavaScript Array object.
var json = [{"id":"1","tagName":"apple"},{"id":"2","tagName":"orange"},{"id":"3","tagName":"banana"},{"id":"4","tagName":"watermelon"},{"id":"5","tagName":"pineapple"}];$.each(json, function(idx, obj) {alert(obj.tagName);});
The code snippet above works normally and prompts "apple", "orange "... As expected.
Question: JSON string
In the following example, a json string (with a single or double quotation mark) is declared directly.
var json = '[{"id":"1","tagName":"apple"},{"id":"2","tagName":"orange"},{"id":"3","tagName":"banana"},{"id":"4","tagName":"watermelon"},{"id":"5","tagName":"pineapple"}]';$.each(json, function(idx, obj) {alert(obj.tagName);});
In Chrome, it displays errors in the console:
Uncaught TypeError: Cannot use 'in' operator to search for '000000'
In [{"id": "1", "tagName": "apple "}...
Solution: convert a JSON string to a JavaScript Object.
To fix it, convert it to a JavaScript Object through standard JSON. parse () or jQuery's $. parseJSON.
var json = '[{"id":"1","tagName":"apple"},{"id":"2","tagName":"orange"},{"id":"3","tagName":"banana"},{"id":"4","tagName":"watermelon"},{"id":"5","tagName":"pineapple"}]';$.each(JSON.parse(json), function(idx, obj) {alert(obj.tagName);});//or $.each($.parseJSON(json), function(idx, obj) {alert(obj.tagName);});
After jquery cyclically traverses the td in tabel by each, how can we integrate the specified td value into an array? First, very urgent
Hello, declare an Array var arr = new Array () outside the each ();
Then, append the value to the each using arr. push (6, 7. You must note that in each, you can use the this keyword to obtain the tag value from this loop.
Example: var arr = new Array ();
$ ("# T1 tbody tr input [name = 'sysnos [] ']: checked"). each (function (I, n ){
P_invNo = $ (n). parent (). parent (). children ("td: eq (1)"). text ();
P_cngNo = $ (n). parent (). parent (). children ("td: eq (2)"). text ();
P_endPrtName = $ (n). parent (). parent (). children ("td: eq (3)"). text ();
Arr. push (p_endPrtName); // append to here, and all values in arr are p_Piece =$ (n ). parent (). parent (). children ("td: eq (5 )"). text ();
P_payenders = $ (n). parent (). parent (). children ("td: eq (6)"). text ();
P_blenders = $ (n). parent (). parent (). children ("td: eq (8)"). text ();
Var p_lodUserName = $ ("# lodUserName"). val ();
});
Guo Libin [authoritative expert]
After jquery cyclically traverses the td in tabel by each, how can we integrate the specified td value into an array? First, very urgent
Var arr = []; $ ("# t1 tbody tr input [name = 'sysnos [] ']: checked "). each (function (I, n) {p_invNo = $ (n ). parent (). parent (). children ("td: eq (1 )"). text (); p_cngNo = $ (n ). parent (). parent (). children ("td: eq (2 )"). text (); p_endPrtName = $ (n ). parent (). parent (). children ("td: eq (3 )"). text (); p_Piece = $ (n ). parent (). parent (). children ("td: eq (5 )"). text (); p_payenders = $ (n ). parent (). parent (). children ("td: eq (6 )"). text (); p_blFee =$ (n ). parent (). parent (). children ("td: eq (8 )"). text (); var p_lodUserName = $ ("# lodUserName "). val (); // put the value you need into the array arr. push (p_endPrtName) ;}); alert (arr); // arr is the array you want