所以我的代码似乎是正确的;但是,组合框只有在刷新页面后才会改变;这不是我对$ .change()的预期。
在当前代码之前,我有两个单独的ajax调用。一个人会清除所有复选框,另一个会设置它们。这没有页面刷新工作;但是,有时会检查不正确的方框(甚至不检查正确的方框)。
我已经通过firebug检查了php的所有回复,结果与预期一致。
代码是jQuery v1.5.1,因为我正在使用jQuery UI。
提前致谢!
PHP
require 'config.php';
if(!empty($_GET['usr'])) {
//Retrieve committee banks list
$db->query('SELECT * FROM clas_gov_app_meeting_access_list WHERE usr="committeebank"');
$committeebank = $db->get();
//Retrieve selected users permissions
$db->query('SELECT * FROM clas_gov_app_meeting_access_list WHERE usr="'. $_GET['usr'].'"');
$accesslist = $db->get();
//Echo user permissions
echo $accesslist[0]['isadmin'];
//Combine committebank's and users permissions sepereated with a slash
$accesslist[0]['accessto'] = $committeebank[0]['accessto'] .',/,' . $accesslist[0]['accessto'];
//Explode on commas
$accesslist = explode(',',$accesslist[0]['accessto']);
//Echo each value
foreach($accesslist as $key => $value) {
echo $value.' ';
}
}
>
JavaScript / jQuery
function SetCheckboxes() {
var usr = $('#KUOID2').find('option:selected').val();
$.ajax({
url : 'ajaxLoadAccess.php?usr=' + usr,
dataType : "text",
success : function(response) {
//first char echoed is a single identifier for admin status
var isAdmin = response.charAt(1);
//clear that from the string
response = response.replace(response.charAt(1), '');
//committebank is first, and seperated from usr by a slash
split_response = response.split("/");
//want to clear checkboxes from the committebank and set from the usr
ClearCheckboxes = split_response[0].split(" ");
SetCheckboxes = split_response[1].split(" ");
//CLEAR CHECKBOXES
//For each value in the array
for(var i in ClearCheckboxes) {
//Skipping over empty checkboxes
if(ClearCheckboxes[i] != '') {
//Check the boxes that need to
if($('input[name='+ClearCheckboxes[i]+'2]').attr('checked')) {
$('input[name='+ClearCheckboxes[i]+'2]').attr('checked', false);
}
}
}
ClearCheckboxes = [];
//SET CHECKBOXES
//For each value in the array
for(var i in SetCheckboxes) {
//Skipping over empty checkboxes
if(SetCheckboxes[i] != '') {
//Check the boxes that need to
$('input[name='+SetCheckboxes[i]+'2]').attr('checked', true);
}
}
SetCheckboxes = [];
if(isAdmin>0) {
$('input[name=isAdmin2]').attr('checked', true);
} else {
$('input[name=isAdmin2]').attr('checked', false);
}
}
});
}
$(document).ready(function() {
$(function() {
$( "#tabs" ).tabs();
});
SetCheckboxes();
$("select").change(function() {
SetCheckboxes();
});
});