[英]jQuery - Transfer Data by ID from PHP to JavaScript + jQuery
我有一個列表,其中包含由(MongoDB-)ID標識的很多條目(100+)。 每個條目都在html表中,並具有一個復選框。 現在,當用戶選擇一個組時,我需要查詢服務器是否每個條目都在特定的組中。 獲取成員資格的查詢並不是很繁重,但是我不能執行100次以上,這是太多的負擔。
目前,我有用於獲取組成員身份的PHP代碼(太長了,無法發布)和以下javascript代碼,只要更改了選擇該代碼便會執行:
$('checkbox[data-type="group"]').each(function(idx, val) {
// get the ID and set it checked/unchecked
});
我的問題是:如果條目在所選組中,如何查詢服務器一次,然后檢查每個ID?
您的問題有點難以理解,但是我認為您應該發布一個JSON列表並將其發布到一個查詢中,並在服務器端處理迭代,如下所示:
id_list = {};
$('checkbox[data-type="group"]').each(function(idx, val) {
the_id = //get the id into a variable somehow;
id_list[the_id] = val;
});
$.ajax({
url: "some url",
dataType: "json",
type: "post",
data:id_list
}).done(function(data) {
//using returned data, set each to check/unchecked
//assuming that the returned data had format of id:boolean, and the boolean defines whether it should be checked (e.g. {1: true, 2: false, 3: false} )
$.each(data, function(index,value) {
if (value == true) {
$('checkbox#'+index).attr('checked',true);
} else {
$('checkbox#'+index).attr('checked',false);
}
});
如果這不能回答您的問題,請更詳細地重述您的問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.