如何通过多个下拉列表'价值回到控制器?
在我的Spring Boot项目的一部分中,有一个页面让用户在不同的下拉列表中选择项目。 由于下拉列表是使用对象列表构建的,因此我不能像在其他地方一样通过其ID传递其ID。 我有什么办法或解决方法可以将所有选定的项目传递给控制器?
<div th:each="group : ${groups}">
<label th:text="${group.name + ': '}"></label>
<select name="name" id ="id">
<option th:each="item: ${group.items}" th:object="${item}" th:value="${item.id}" th:text="${item.name}"></option>
</select>
<br>
</div>
In part of my spring boot project, there is a page let user to select items in different drop down lists.
Since the drop down lists are build with a list of object, I cant pass the selected result with their id as I do in other place.
Is there any way or workaround for me to pass all the selected items to my controller?
<div th:each="group : ${groups}">
<label th:text="${group.name + ': '}"></label>
<select name="name" id ="id">
<option th:each="item: ${group.items}" th:object="${item}" th:value="${item.id}" th:text="${item.name}"></option>
</select>
<br>
</div>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我可以为您提供我使用的假设解决方案,但您必须自己实施其余的解决方案。
在我的模板中,我有一个下拉列表,我的工作是添加了一个on Change操作。这将我从下拉列表中选择的任何内容添加到隐藏的输入字段。添加完成后,我将在文本字段中发送数据。
在我的代码中,我向所有输入添加了一个_在我的Java中,我将使用string.split()方法分析。您不必添加一个_,但是就我而言,我有一个单一字符的选择,如果您添加特殊字符,我也可以更轻松地拆分。
希望这会有所帮助。
I can give you a hypothetical solution that im using but you will have to implement the rest yourself.
In my templating, I have a dropdown and what I did is added an onchange action. This adds whatever I selected from the dropdown to a hidden input field. Once i'm done adding, I send the data in the text field.
In my code i added a _ to all the inputs to in my java I would parse with string.split() method. You dont have to add a _ but in my case I had options that were singular characters and also I makes it easier to split if you add a special character.
Hope this helps.