how to submit a form without losing values already selected at the same form

耗尽温柔 提交于 2019-12-08 10:54:20

问题


I am using jstl with dropdown lists.

When i click submit button i success the specification but values int dropdownlists are reinitialized.

So I want to submit form without loosing the values already selected in the form because I need to stay always at the same level in the form.To be more clear, user choose a value from ddl and click edit button to show other options and fill them at the same form without loosing what he has selected.

I have tried to deal like that...

<form action="myjsp.jsp" method="post">
<input type="Submit" value="Edit">

...but it doesn't work.

Thank you for your help.


回答1:


You need to preset the inputs with the request parameter values. You can access parameter values in EL by ${param.name}. Basically:

<input type="text" name="foo" value="${param.foo}">

Note that this is XSS sensitive. You always need to sanitize the user inputs. You can use the JSTL functions taglib for this.

<input type="text" name="foo" value="${fn:escapeXml(param.foo)}">

In case of dropdowns rendered by HTML <select> element, it's a bit trickier. You need to set the selected attribute of the HTML <option> element in question. You can make use of the ternary operator in EL to print the selected attribute whenever the option value matches the request parameter value.

Basic example:

<select name="foo">
   <c:forEach items="${options}" var="option">
       <option ${param.foo == option ? 'selected' : ''}>${option}</option>
   </c:forEach>
</select>


来源:https://stackoverflow.com/questions/2710344/how-to-submit-a-form-without-losing-values-already-selected-at-the-same-form

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!