我有一個系統,其中幾個選擇動態填充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