我有一个系统,其中几个选择动态填充ajax调用一个servlet。这些选项在servlet上生成,该选项确定启用或禁用哪些选项。无法启用动态生成的选择选项
一个选择,我需要启用一个或多个标记为禁用的选项。但是,我似乎无法在ajax调用后启用它们。起初我认为这可能是一个异步问题,但我处理了这个问题,但仍然无法启用它们。 (“禁用”,假),尝试.attr(“禁用”,假),并尝试.removeAttr(“禁用”)。这些都没有工作。
呼叫的HTML。对于selEditRun的选项也动态生成的,对应于 “运行” 的数据库ID值:
<select id="selEditRun" onchange="populateEditRun()">
<option value=""> </option>
</select>
的Javascript:
function populateEditRun() {
var id = $('#selEditRun').val();
$.ajax({
url: 'TCAUServlet',
type: 'GET',
data: {
formType: 'getRun',
id: id
},
dataType: 'html',
success: function (responseText) {
responseText = $.parseJSON(responseText);
var deputies = responseText.officersAssigned;
//other field actions removed
var tmpDate = $('#txtEditRunDateTime').val();
$.when(updateDisablesLists(tmpDate)).done(function() {
enableOptions(deputies);
});
},
error: function (request, status, error) {
alert("An error occurred. Error: " + status + ", " + error);
}
});
}
function updateOfficerWithDisables(d) {
$.ajax({
url: 'TCAUServlet',
type: 'GET',
data: {formType: "getOfficersWithDisables",
date: d
},
dataType: 'html',
success: function (responseText) {
$('#selEditRunDeputies').html(responseText);
//other select population removed
},
error: function (request, status, error) {
alert("An error occurred. Error: " + status + ", " + error);
}
});
}
function enableOptions(deputies) {
var tmpArray = [];
$.each(deputies, function (name, value) {
tmpArray.push(value.id); //get option values that are to be enabled
$("#selEditRunDeputies option[value='" + value.id + "']").attr("disabled", false); //set to enabled
});
$('#selEditRunDeputies').val(tmpArray); //set the appropriate field(s) selected)
}
function updateDisablesLists(val) {
var d = convertDatePickerToDate(val);
updateOfficerWithDisables(d);
}
终于在这个servlet:
private String getOfficersWithDisables(HttpServletRequest request) throws ClassNotFoundException, SQLException, ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("dd MMM yyyy");
String htmlString = "<option value=''> </option>\n";
List<Officer> officerList = TCAUDatabaseUtil.getOfficers();
String dateString = request.getParameter("date");
Date d = sdf.parse(dateString);
Calendar cal = Calendar.getInstance();
cal.setTime(d);
int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK);
for (Officer officer : officerList) {
boolean hasRun = !(TCAUDatabaseUtil.getRunName(d, officer.getId()).equals(""));
boolean hasAppt = TCAUDatabaseUtil.hasAppt(d, officer.getId());
boolean isReserveDay = (dayOfWeek == convertTCAUDayToCalendarDay(officer.getRd1()) || dayOfWeek == convertTCAUDayToCalendarDay(officer.getRd2()));
boolean isAssigned = !(TCAUDatabaseUtil.getAttendanceEvent(d, officer.getId()).equals(""));
boolean isDisabled = (hasAppt || hasRun || isReserveDay || isAssigned);
htmlString += "<option value='" + officer.getId()+ "'" + (isDisabled?" disabled":"") + ">" + officer.getDisplayName() + "</option>";
}
return htmlString;
}
我意识到这是很多代码张贴(因为张贴了太多的代码在这里大吼),但我不知道我在哪里搞砸了。
当你解析'json'作为响应时,为什么你的'datatype:'html''?另外,如果值为'null'或者“'”,您从AJAX调用中得到的响应是什么? – Twisty
@Twisty:ajax调用返回一个“运行”对象。我认为“datatype:html”是一个复制粘贴错误,但它仍然有效,所以我没有注意到它。值不会为空或“”,除非有人手动进入数据库并开始删除东西。 –
如果我理解操作,用户选择一个日期,这将反过来启用或禁用select元素中的多个选项,以使它们不能分配到该日期。这是沿着正确的道路工作吗? – Twisty