var ciContact = data.split(", ");
var tmpHash = {};
for(var i = 0; i < ciContact.length; i++){
tmpHash[$.trim(ciContact[i])] = 1; // if you are pretty sure that your data is clean, skip $.trim()
}
$('input[name="ciContact[]"]').each(function(){
if($(this).val() in tmpHash){
$(this).attr('checked', 'true');
}
}
или существует новая библиотека javascript под названием tog, вы можете использовать ее для обработки ajax.
function checkBox(data, all_options){
var ciContact = data.split(", ");
var tmpHash = {};
for(var i = 0; i < ciContact.length; i++){
tmpHash[$.trim(ciContact[i])] = 1; // if you are pretty sure that your data is clean, skip $.trim()
}
return Tog().map(all_options, function(opt, key, _num){
var t = tog.Td().Tog('label').checkbox('$ciContact[]')
.id('ciContact_', _num);
if(opt in tmpHash){ t.checked() }
// close the check box, and append the value as its label
t.close().cont(opt);
return t;
}).html();
}
Вызов checkBox (данные, опции) даст вам кусок html.
и вы можете добавить его к чему-то вроде $ ('таблица tr')
На вашем бэкэнде вам просто нужно выгрузить все ваши опции в строку json.
И ваша клиентская сторона может легко получить это, так называемые «all_options»