2012-03-25 42 views
0

我有以下的JavaScript我使用保存選中的複選框元素陣列。「未定義」數組中

不知道這裏出現了什麼問題。

+2

祝賀,你已經設法幾乎完全避免編寫任何jQuery。 – 2012-03-25 16:41:02

回答

1

它在jQuery的POST方法,這是錯誤的數據屬性相關聯。它不能將數組作爲參數。這裏是文檔

數據映射或字符串與請求發送到服務器。使用對象文本,而不是

嘗試:

$.post('./Courses/SaveAndRedirect', {selectedIDs: selectedIDs}, function (data) { }); 

我也試着寫selectedIDs[x] = a[i].value;不同:

selectedIDs.push(a[i].value); 
0

我認爲這個問題可能是您的文章變量只是一個數字實例,而不是一個字段名

0

你可以嘗試這樣的事情:

var selectedIDs = []; 
$('input[type="checkbox"]:checked').forEach(function(i, e){ 
    selectedIDs.push($(e).val()); 
}); 
$.post('./Courses/SaveAndRedirect', selectedIDs, function (data) { });