如何通过多个下拉列表'价值回到控制器?

发布于 2025-02-10 07:58:36 字数 487 浏览 1 评论 0原文

在我的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 技术交流群。

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

发布评论

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

评论(1

违心° 2025-02-17 07:58:36

我可以为您提供我使用的假设解决方案,但您必须自己实施其余的解决方案。

在我的模板中,我有一个下拉列表,我的工作是添加了一个on Change操作。这将我从下拉列表中选择的任何内容添加到隐藏的输入字段。添加完成后,我将在文本字段中发送数据。

    <select class="exempt" onchange="addProgrammingLanguages(this, 'dropId', 'langtxt');">
      <option value="Select">Select languages *</option>
      <option value="Java">Java</option>
      <option value="Javascript">Javascript</option>
      <option value="Python">Python</option>
      <option value="C">C</option>
      <option value="C#">C#</option>
    </select>
    <div class="exempt" id="dropId" style="max-width:80%; border:none; height:auto; display:flex; flex-direction:row; gap:5px; flex-wrap:wrap;">

    </div>
    <input id="langtxt" type="hidden" th:field="*{language}">
function addProgrammingLanguages(menu, ident, txtfield)
{
    var selected = menu.value;

    button.onclick = function() {
        removeMenu(this.id, ident);
        document.getElementById(txtfield).value = document.getElementById(txtfield).value.replace(this.id.split("_")[0]+"_",'');
     };


    document.getElementById(txtfield).value += selected+'_';
    menu.selectedIndex=0;
}

在我的代码中,我向所有输入添加了一个_在我的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.

    <select class="exempt" onchange="addProgrammingLanguages(this, 'dropId', 'langtxt');">
      <option value="Select">Select languages *</option>
      <option value="Java">Java</option>
      <option value="Javascript">Javascript</option>
      <option value="Python">Python</option>
      <option value="C">C</option>
      <option value="C#">C#</option>
    </select>
    <div class="exempt" id="dropId" style="max-width:80%; border:none; height:auto; display:flex; flex-direction:row; gap:5px; flex-wrap:wrap;">

    </div>
    <input id="langtxt" type="hidden" th:field="*{language}">
function addProgrammingLanguages(menu, ident, txtfield)
{
    var selected = menu.value;

    button.onclick = function() {
        removeMenu(this.id, ident);
        document.getElementById(txtfield).value = document.getElementById(txtfield).value.replace(this.id.split("_")[0]+"_",'');
     };


    document.getElementById(txtfield).value += selected+'_';
    menu.selectedIndex=0;
}

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.

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