无法启用动态生成的选择选项

时间:2017-03-15 19:34:03

标签: javascript jquery ajax servlets

我有一个系统,通过对servlet的ajax调用动态填充多个选择。这些选项在servlet上生成,用于确定启用或禁用哪些选项。

一个选择,我需要启用一个或多个标记为禁用的选项。但是,我似乎无法在ajax调用后启用它们。起初我以为这可能是一个异步问题,但我处理了这个问题,但仍然无法启用它们。

我尝试过.prop(“禁用”,false),尝试.attr(“禁用”,false),然后尝试.removeAttr(“禁用”)。这些都没有奏效。

通话的HTML。 selEditRun的选项也是动态生成的,其值对应于“run”的数据库id:

    <select id="selEditRun" onchange="populateEditRun()">
        <option value="">&nbsp;</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=''>&nbsp;</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;
}

我意识到这是很多代码要发布(因为发布过多代码而被大吼大叫),但我不知道我在哪里搞砸了。

1 个答案:

答案 0 :(得分:0)

我无法弄清楚你试图解释的逻辑。没什么大不了的,问题归结为启用和禁用选项。我认为您的问题与格式化有关。

请考虑以下事项:

-- SYSTIMESTAMP_COL                   15/03/2017 16:01:14
-- SYSDATE_COL                        15/03/2017 19:01:14
-- CURRENT_TIMESTAMP_COL              15/03/2017 16:01:14
-- DATE_COL                           15/03/2017 19:02:00

这是有效的,也是速记。 <select> <option>1</option> <option disabled>2</option> </select> 元素具有以下属性(link):

option

更有效的是以下内容:

Attribute  Value     Description
---------------------------------
disabled   disabled  Specifies that an option should be disabled
label      text      Specifies a shorter label for an option
selected   selected  Specifies that an option should be pre-selected when the page loads
value      text      Specifies the value to be sent to a server

现在,您可以对此致电<select> <option>1</option> <option disabled="disabled">2</option> </select> 并使用此方法将.prop()设置为disabledtrue

工作示例:https://jsfiddle.net/Twisty/2xj18jk9/

<强> ASP

false

<强>的JavaScript

private String getOfficersWithDisables(HttpServletRequest request) throws ClassNotFoundException, SQLException, ParseException {
    SimpleDateFormat sdf = new SimpleDateFormat("dd MMM yyyy");
    String htmlString = "<option value=''>&nbsp;</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='disabled'":"") + ">" + officer.getDisplayName() + "</option>";
    }
    return htmlString;
}

希望有所帮助。