简体   繁体   English

如何维护jsp中select标签中的selected选项?

[英]How do I maintain the selected option from select tag in a jsp?

I've read many solutions to the above question, but my specific problem has some complications. 我已经阅读了上述问题的许多解决方案,但是我的特定问题有些复杂。 I have a select tag whose options are filled 1-20 using a JSTL forEach tag, and I want the option that was previously selected to be automatically selected when the user revisits the page. 我有一个select标记,它的选项使用JSTL forEach标记填充了1-20个选项,我希望当用户重新访问页面时可以自动选择以前选择的选项。 The select tag is within another forEach for the items within a list type attribute, so the select 1-20 is repeated many times on the page. select标记位于列表类型属性中项目的另一个forEach内,因此select 1-20在页面上重复多次。

Image of the page 页面图片

在此处输入图片说明

The page is for selecting items to add to a cart, and the select 1-20 is for the quantity. 该页面用于选择要添加到购物车的物品,选择1-20用于数量。 I want this quantity to stay consistent when the user revisits the page after submitting its form. 我希望用户提交表单后重新访问页面时,此数量保持一致。 I've already accomplished something similar where the checkbox for each item stays checked upon revisit, thanks to an answer to another question that suggested stream() using paramValues. 由于对另一个建议使用paramValues建议stream()的问题的回答,我已经完成了类似的工作,其中每个项目的复选框在再次访问时都保持选中状态。

I tried to use a similar approach for the select 1-20, but it did not work. 我尝试对选择的1-20使用类似的方法,但是没有用。 I thought about using javascript, with which I have very little experience, but this is complicated due to my select name, out of necessity, being a unique ID for each item on the page, assigned using EL. 我考虑过使用javascript,但经验很少,但是由于我的选择名称(由于使用EL分配给页面上每个项目的唯一ID)而不必要,所以这很复杂。

This is the code as is without a way to keep the previously selected quantity, but successfully keeps previously selected items checked: 这是没有保留以前选择的数量的方法的代码,但是成功保留了以前选择的项目的检查方式:

<c:forEach var="item" items="${items}">
    <c:if test="${item.category == 'Burger'}">
        <tr>
            <td><input type="checkbox" name="itemID" value="${item.itemID}" ${paramValues.itemID.stream().anyMatch(v->v == item.itemID).get() ? 'checked' : ''}></td>
            <td>
                <select name="${item.itemID}">
                    <c:forEach var="i" begin="1" end="20">
                        <option value="${i}">${i}</option>
                    </c:forEach>
                </select>
            </td>
            <td>${item.name}</td>
            <td><fmt:formatNumber value="${item.price}" type="currency"/></td>
        </tr>
    </c:if>
</c:forEach>

My unsuccessful attempt to use stream() for the select 1-20: 我对选择的1-20使用stream()的尝试失败:

<c:forEach var="i" begin="1" end="20">
    <option value="${i}" ${paramValues.item.itemID.stream().anyMatch(v->v == i).get() ? 'selected' : ''}>${i}</option>
</c:forEach>

The above solution was taken from How can I retain HTML form field values in JSP after submitting form to Servlet? 上面的解决方案来自提交表单到Servlet之后如何在JSP中保留HTML表单字段值? where the given example code from an answer: 给出的示例代码来自答案:

<select name="boo" multiple>
    <option value="a" ${paramValues.boo.stream().anyMatch(v->v == 'a').get() ? 'selected' : ''}>label a</option>
    <option value="b" ${paramValues.boo.stream().anyMatch(v->v == 'b').get() ? 'selected' : ''}>label b</option>
    <option value="c" ${paramValues.boo.stream().anyMatch(v->v == 'c').get() ? 'selected' : ''}>label c</option>
</select>

didn't even work when I tried shoving it in my code just to test, and it doesn't even have the complication of being with a forEach, etc. I'm not sure why, as the same solution worked for my checkbox, as seen in my first block of code. 当我尝试将其插入代码以进行测试时,它甚至都无法正常工作,甚至没有forEach等的复杂性。我不确定为什么,同一个解决方案适用于我的复选框,如我的第一段代码所示。

Thank you very much for any help. 非常感谢您的帮助。

It's because you used a dynamic name for the HTML element: 这是因为您为HTML元素使用了动态名称:

<select name="${item.itemID}">

and you're using a hardcoded name while inspecting the parameter values: 并且您在检查参数值时使用了硬编码名称:

${paramValues.item.itemID}

This basically looks for the request parameter values with the literal name "item", and then attempts to treat it as a javabean in order to grab the literal property name "itemID". 这基本上是用文字名称“ item”寻找请求参数值,然后尝试将其视为javabean以便获取文字属性名称“ itemID”。 Under the covers, it has the same effect as request.getParameterValues("item").getItemID() . 在幕后,它的作用与request.getParameterValues("item").getItemID() This is not correct. 这是不正确的。 You need to look for request parameter values with the dynamic name ${item.itemID} instead. 您需要查找具有动态名称${item.itemID}请求参数值。 You basically want to end up with the same effect as request.getParameterValues(item.getItemID()) . 您基本上希望最终获得与request.getParameterValues(item.getItemID())相同的效果。

${paramValues[item.itemID]}

In other words, this must work for you: 换句话说,这必须为您工作:

<option value="${i}" ${paramValues[item.itemID].stream().anyMatch(v->v == i).get() ? 'selected' : ''}>${i}</option>

See also: 也可以看看:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在选择jsp中的select标记中的选项时从数据库中检索数据 - How to retrieve data from the database when an option in a select tag in the jsp is selected (MVC计算器)如何保持从index.jsp中选择一个选项多少次的值? - (MVC Calculator) How to maintain the value of how many times an option from index.jsp is selected? 如何从一个jsp页面获取选项标签名称以选择另一个jsp页面的标签 - how to get option tag name from one jsp page to select tag of another jsp page 每次我从选择选项中选择一个选项时都会运行一个 jsp 自定义标记 - making a jsp custom tag run each time i select an option from a select option 如何在JSP中使用选定的属性预填充选择选项元素 - How to Pre-populate Select option element with Selected atribute in JSP 如何在jsp的select选项中选择数据库值? - How to get database value selected in select option in jsp? 选择动态选择的选项:使用JSP + Jquery - Select option selected dynamically: Using JSP + Jquery 如何将复选框中的选定参数从一个jsp页面传递到另一个jsp页面? - how do I pass the selected parameters in the checkbox from one jsp page to another jsp page? 使用select和option标签在jsp中插入数据 - Inserting a data in jsp by using select and option tag 如何从jsp中的选择框(选中和未选中)获取所有值 - How to get all values from a select box(selected & not selected) in jsp
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM