【问题标题】:Ajax how can I refresh a drop down after call servletAjax如何在调用servlet后刷新下拉列表
【发布时间】:2026-02-17 21:25:02
【问题描述】:

我有以下jsp

<c:forEach items="${vlocomotoras}" var="vl" varStatus="i">

    <select class="regimenFrenoLocomotora" id="regimenFrenoLocomotora" name="regimenFrenoLocomotora" onchange="calcularDocumentoTren();">
        <option value="P" ${"P" == v1.regimenFrenoLocomotoras ? 'selected="selected"' : ''}>P</option>
        <option value="G" ${"G" == v1.regimenFrenoLocomotoras ? 'selected="selected"' : ''}>G</option>

</select>

</c:forEach>

我有以下 ajax 发布到 servlet

$.ajax({
            type: "POST",
            url: "/copernico/SrvRenfeSituacion",
            data: {
            "arrayLocomotora[]" : arrayLocomotora,
            todo:todo,
            fecha:fecha,
            tren:tren
          },
            error:function(){
            console.log("ERROR");
          },
            success:function(responseText){
            alert('responseText ');
            alert(responseText);
            alert("Success");
          }
        });

这是小服务程序

req.setAttribute("vlocomotoras", locomotoras);

现在我想用 vlocomotoras 的新值刷新下拉列表。我该怎么做?

【问题讨论】:

    标签: java jquery ajax jsp servlets


    【解决方案1】:

    让您的 servlet 返回一个包含您需要的数据的 json 数组。 See this question for doing that.

    (在 ajax 调用中,为调用设置 dataType="json" 以期望 json 对象作为响应)

    在 ajax 响应中获得 json 对象后,您可以使用 jQuery(empty()appendTo())替换 SELECT 内的 OPTION 标签。

    【讨论】: