javascript 表单提交后如何在下拉框中保留选定的值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27447430/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to keep selected value in dropdown box after form submission?
提问by Ievgen
Out of the many solutions I've found, I can't seem to get any of them to work for me. I've got a dropdown list in my jsp file:
在我找到的许多解决方案中,我似乎无法让它们中的任何一个对我来说有效。我的 jsp 文件中有一个下拉列表:
<select name="chosenOne" onchange="javascript:getUsers(this.value);">
<option value="0" onclick="javascript:getUsers(this.value);">All Modules</option>
<c:forEach items="${modules}" var="module">
<option value="${module.id}"><c:out value="${module.title}"/></option>
</c:forEach>
</select></p>
It populates dynamically from my database, except for the "All Modules" option. Here's my javascript function for the onchange event:
它从我的数据库动态填充,除了“所有模块”选项。这是我的 onchange 事件的 javascript 函数:
<script type="text/javascript">
function getUsers(id) {
if (id != "0"){
document.updateForm.id.value = id;
}
else{
document.updateForm.id.value = "0";
}
document.updateForm.submit();
}</script>
And here's my servlet code that deals with the dropdown box amongst other things:
这是我的 servlet 代码,用于处理下拉框等:
protected void process(final HttpServletRequest request, final HttpServletResponse response)
throws ServletException, IOException {
long modID = 0;
String url = "jsp/user/administration.jsp";
request.setAttribute("modules", dataAccessor.getAllModules());
if (isParameterValid(request.getParameter("id"))) {
modID = Long.parseLong(request.getParameter("id"));
request.setAttribute("users", getUsersFromModule(modID));
System.out.println(modID);
} else if (!isParameterValid(request.getParameter("id"))) {
request.setAttribute("users", dataAccessor.getAllUsers());
} else {
request.setAttribute("errorMessage", "There was a problem retrieving users!");
url = "jsp/error.jsp";
}
//request.setAttribute("formerSelect", modID);
request.getRequestDispatcher(url).forward(request, response);
}
So how can I get the selected dropdown value to remain in the dropdown box after the form refreshes? I've fiddled around with setting an attribute "formerSelect" which just contains the value of the previously selected item in the dropdown. But then for some reason it rendered my dropdown useless when I tried to assign it to the "selected" value within my options tag. Any help is much appreciated.
那么如何在表单刷新后让选中的下拉值保留在下拉框中呢?我已经设置了一个属性“formerSelect”,它只包含下拉列表中先前选择的项目的值。但是由于某种原因,当我尝试将它分配给我的选项标签中的“选定”值时,它使我的下拉菜单变得无用。任何帮助深表感谢。
回答by Ievgen
You need to pass this parameter to the httpRequest after submitting:
提交后需要将此参数传递给httpRequest:
request.setAttribute("selectedModule", request.getParameter("chosenOne"));
and after that you need to mark an option as selected:
之后,您需要将一个选项标记为选中:
<c:forEach items="${modules}" var="module">
<option value="${module.id}" ${module.id == selectedModule ? 'selected':''}>...</option>
</c:forEach>
回答by Madhuka Dilhan
I can explain like this. As a example code put this code in your Servlet
我可以这样解释。作为示例代码,将此代码放在您的 Servlet 中
String status = request.getParameter("status");
request.setAttribute("status", status);
put this code in jsp file
把这段代码放在jsp文件中
<select id="status" name="status" class="listBx" onChange = "check(this);">
<option value="" >--- Select ---</option>
<option value="1" <%if((request.getAttribute("status") != null) && request.getAttribute("status").equals("1")){ %> selected <%} %>>Logged in</option>
<option value="0" <%if((request.getAttribute("status") != null) && request.getAttribute("status").equals("0")){ %> selected <%} %>>Logged Out</option>
</select>