Here is my Array
, this comes out of a jQuery
.post
success function
:
data = JSON.parse(data);
$(".chk_permission_").each(function() {
if ($.inArray($(this).val(), data)) {
$(this).prop("checked", true);
} else {
$(this).prop("checked", false);
}
});
when I alert
data, I get the correct two values. I have ten checkboxes, all with class chk_permission_
. Two of the values of these checkboxes contain the data in the array. however when I run the above code: For all users that return ANYThing back in the data
array, the first box is unchecked, the rest is checked. For everyone else, that returns null (which means no boxes should be checked), all ten boxes are checked.
What am I doing wrong?
$.inArray() returns the index of the item in the array if it is found else returns -1
data = JSON.parse(data);
$(".chk_permission_").each(function () {
$(this).prop("checked", $.inArray($(this).val(), data) > -1);
});
so if the item is found in position 0
, it returns 0
which is a falsy value so instead of the if
block else
block will get executed
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.