问题
I have the following json array returning from an ajax call:
{"err":"err_type","fields":["field1","field2"]}
when tryin to print it out with this function:
$.each(data.fields, function (i, field) {
console.log(field);
$.each(field, function (j, f) {
$('[name="'+f+'"]').addClass('form_err');
console.log(f);
});
});
i get this:
data1
TypeError: invalid 'in' operand a
...turn function(b){return db(a,b).length>0}}),contains:fb(function(a){return funct...
and so i can't figure out how to use this array! Anyone have any idea?
回答1:
You are iterating a string, you don't need two .each() functions
$.each(data.fields, function (i, field) {
$('[name="'+field+'"]').addClass('form_err');
console.log(field);
});
回答2:
Remember to add dataType: 'json';
so that it can be looped as an array and not a string.
回答3:
You don't need another loopin $.each()
:
$.each(data.fields, function (i, field) {
console.log(field);
$('[name="'+field+'"]').addClass('form_err');
});
data.fields
is an array which contains strings
only, so you don't need to loopin again. You just need to loopin only when if it would be another array object with {key:value}
pairs.
回答4:
In your main loop, the "field" var is not an array, so you can't use "each" on it.
来源:https://stackoverflow.com/questions/24429957/jquery-typeerror-invalid-in-operand-a