如何让JSP中提交后dropdownlist中的值不改变?

发布于 2024-07-30 05:52:59 字数 119 浏览 3 评论 0原文

假设我有一个包含下拉列表的 jsp 页面。 当用户选择一个项目并单击“提交”将JSP页面提交给自己时, 之后,JSP 页面将重新加载,之前选择的项目将被取消选择。 我怎样才能使它即使在重新加载JSP页面后也不会改变?

Let's say i have a jsp page which contains a dropdownlist.
when user select a item and click submit to submit the JSP page to itself,
after that , the JSP page will reload and item selected before will disselected.
How can i make it not change even after reload the JSP page?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

屋顶上的小猫咪 2024-08-06 05:53:00

你必须重新选择它。

所以它最终会像这样:

 <select name="dropdown">
 <%
       String selectedItem = request.getParameter("dropdown");
       for( String item : values ) {
 %>
       <option <%=item.equals(selectedItem)?selected:""%>><%=item%>

 <%
       }
 %>

这样每次重新加载 jsp 页面时,您都会验证当前正在绘制的项目是否与用户之前选择的相同。 如果是这样,你将“selected”附加到选项中(第一次它不会匹配任何内容)

我在JSP中有点生疏,所以,可能有一种更“优雅”的方法来做到这一点,但是这个“旧的” -style”确实有效。

我希望它有帮助。

You have to re-select it.

So it will end up like this:

 <select name="dropdown">
 <%
       String selectedItem = request.getParameter("dropdown");
       for( String item : values ) {
 %>
       <option <%=item.equals(selectedItem)?selected:""%>><%=item%>

 <%
       }
 %>

That way each time you reload the jsp page, you verify if the current item you're painting is the same it was previously selected by the user. If so, you append "selected" to the option ( the first time it won't match anything )

I'm a bit rusty in JSP, so , probably there is a more "elegant" way to do it, but this "old-style" does works for sure.

I hope it helps.

最美不过初阳 2024-08-06 05:53:00

我认为这个方法更好:

<script type = "text/javascript"/>
<%String selectedItem ;
if(request.getAttribute("dropdown") != null){
  selected= request.getAttribute("dropdown");%>

document.getElementById("selectbox").selectedIndex = selectedItem;
<%}%>
</script>

我认为这个方法会起作用。

I think this method is better:

<script type = "text/javascript"/>
<%String selectedItem ;
if(request.getAttribute("dropdown") != null){
  selected= request.getAttribute("dropdown");%>

document.getElementById("selectbox").selectedIndex = selectedItem;
<%}%>
</script>

I think this method will work.

一身软味 2024-08-06 05:53:00

设置传入表单的 optionselected 属性...您的输出应如下所示(假设用户选择“Saab”):

<select>
  <option>Volvo</option>
  <option selected="selected">Saab</option>
  <option>Mercedes</option>
  <option>Audi</option>
</select>

Set the selected attribute of the option that was passed in to the form... your output should look like this (assuming the user selected "Saab"):

<select>
  <option>Volvo</option>
  <option selected="selected">Saab</option>
  <option>Mercedes</option>
  <option>Audi</option>
</select>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文