我想用ajax這樣無法使用AJAX
$('#category').click(function(event) {
category = $("input[name='category']:checked").data("id");
var Html = "";
$.ajax({
type: "POST",
data: {
'csrfmiddlewaretoken': '{{csrf_token}}',
category: category
},
url: "{% url 'data_entry' %}",
success: function(data) {
console.log(data);
$.each(data, function(key, object) {
$.each(object, function(key, values) {
Html = "<div><label>" + object + "</label>"
$.each(values, function(key, value) {
Html += "<div style='float:left'><input name='" + value + "' type='checkbox' value='" + value + "'><span>" + value + "</span></div>"
});
});
});
console.log(Html)
$('#attribute').html(Html)
},
error: function(response, error) {}
});
});
結果我得到得到的結果存儲結果是一個數組,看起來像這樣
[Object, Object]
0
:
Object
color
:
Array[3]
0
:
"BLUE"
1
:
"RED"
2
:
"YELLOW"
length
:
3
__proto__
:
Array[0]
__proto__
:
Object
1
:
Object
size
:
Array[6]
0
:
"S"
1
:
"M"
2
:
"L"
3
:
"XL"
4
:
"XXL"
5
:
"XXXL"
length
:
6
__proto__
:
Array[0]
__proto__
:
Object
length
:
2
__proto__
:
Array[0]
我試圖環通過數組的所有值並將其存儲在變量Html中,但它只顯示最終的迭代值,即大小數組中存在的值。什麼可能是錯誤的,我該如何解決此錯誤。
順便數據,您需要添加'HTML + ='
如何從javascript對象中獲取像大小和顏色的數組名稱? –