2017-03-15 98 views
0

我有一个系统,其中几个选择动态填充ajax调用一个servlet。这些选项在servlet上生成,该选项确定启用或禁用哪些选项。无法启用动态生成的选择选项

一个选择,我需要启用一个或多个标记为禁用的选项。但是,我似乎无法在ajax调用后启用它们。起初我认为这可能是一个异步问题,但我处理了这个问题,但仍然无法启用它们。 (“禁用”,假),尝试.attr(“禁用”,假),并尝试.removeAttr(“禁用”)。这些都没有工作。

呼叫的HTML。对于selEditRun的选项也动态生成的,对应于 “运行” 的数据库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; 
} 

我意识到这是很多代码张贴(因为张贴了太多的代码在这里大吼),但我不知道我在哪里搞砸了。

+0

当你解析'json'作为响应时,为什么你的'datatype:'html''?另外,如果值为'null'或者“'”,您从AJAX调用中得到的响应是什么? – Twisty

+0

@Twisty:ajax调用返回一个“运行”对象。我认为“datatype:html”是一个复制粘贴错误,但它仍然有效,所以我没有注意到它。值不会为空或“”,除非有人手动进入数据库并开始删除东西。 –

+0

如果我理解操作,用户选择一个日期,这将反过来启用或禁用select元素中的多个选项,以使它们不能分配到该日期。这是沿着正确的道路工作吗? – Twisty

回答

0

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

考虑以下几点:

<select> 
    <option>1</option> 
    <option disabled>2</option> 
</select> 

这是有效的但也是简写。该option元素具有以下属性(link):

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()并使用此设置disabledtruefalse方法。

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

ASP

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; 
} 

的JavaScript

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 + "']").prop("disabled", false); //set to enabled 
    }); 
    $('#selEditRunDeputies').val(tmpArray); 
} 

希望有所帮助。

+0

我得到相同的结果。但是,我想确保我获得了有效的ID,因此我将alert(value.id)作为enableOptions中循环的第一行。令我惊讶的是,一切都按预期工作。我警戒了,它停止了工作。即使在那里完成时,updateDisablesList中的加载是否可以完成? –

+1

我怀疑它是。可以看看https://api.jquery.com/jquery.deferred/ – Twisty