【问题标题】:Spring MVC get value of onclick input star ratingSpring MVC获取onclick输入星级的值
【发布时间】:2015-02-06 09:31:42
【问题描述】:

我正在编写一个 java spring mvc web 应用程序,在客户端我使用引导程序和一个星级插件来表示对我向用户显示的歌曲列表的欣赏。

我正在做的是传递我在会话中生成的播放列表,并在 jsp 中迭代此列表以创建一个适合的表。在歌曲名称\标题旁边我想显示一个星级系统;我做到了,但它被设置为输入数字。我想要做的是,点击星号,重定向到另一个页面,在控制器中检索速率,进行一些逻辑,然后返回没有那首歌的播放列表页面。

我遇到了一些困难,因为在控制器上,要了解哪首歌被评分,我显然必须将歌曲的 id 作为输入参数,所以输入的名称类似于“input2838” ,但是在控制器端,当我必须请求具有RequestParameter 值的参数时,我需要准确指定我想要的参数的名称,所以我认为这不是正确的做法。

我向您询问有关如何执行此类操作的建议,最重要的是,当我单击该输入时,如何重定向到另一个页面,传递参数的值...

我向你展示了我在其中显示歌曲表的代码的 jsp sn-p。

<script>$("#input-id").rating();</script> <-- initialize the stars script

<c:choose>    <-- when the playlist is not empty show it
  <c:when test="${not empty playlist}">
   <div class="container col-lg-10 col-lg-offset-1" >
    <table class="table table-striped table-bordered table-hover">

        <thead>
            <tr>
                <th>#</th>
                <th>Cover</th>
                <th>Artista</th>
                <th>Titolo</th>
                <th>Rate</th>
            </tr>
        </thead>

        <tbody>

<c:forEach items="${playlist}" var="canzone" varStatus="count">
<tr style="font-weight: normal">
  <td style="font-weight: normal">${count.index +1}</td>
  <td style="font-weight: normal"><img src="${canzone.albumImageUrl}" /></td>
  <td style="font-weight: normal">${canzone.artistName}</td>
  <td class="col-lg-4" style="font-weight: normal">${canzone.title}</td>
<td class="col-lg-4" style="font-weight: normal"><div><input id="input-id" type="number" class="rating" min=0 max=5 step=1 data-size="xs" data-rtl="false"></div></td>
       <tr>
    </c:forEach>
            </tbody>

    </table>
    </div>
  </c:when>
  <c:otherwise>

为了完整起见,现在我将星级 js 发布为 pastebin 链接:

Pastebin here

不要犹豫,询问其他信息。

【问题讨论】:

    标签: java javascript html spring controller


    【解决方案1】:

    您的问题是,如果您获得表中指定为id="sondId"name="sondId" 的所有歌曲ID,并且使用request.getParameter("songId"),它不会选择正确的值,因为所有行的值都将包含相同id 和 name。

    所以你需要做的是在 &lt;c:forEach&gt; 里面输入 a ,如下所示

    <c:forEach items="${playlist}" var="canzone" varStatus="count">
        <form action="yourActionPath" method="post">
            <tr style="font-weight: normal">
                <input type="hidden" id="songId" name="songId" value="${canzone.id}"/>
                <td style="font-weight: normal">${count.index +1}</td>
                <td style="font-weight: normal"><img src="${canzone.albumImageUrl}" /></td>
                <td style="font-weight: normal">${canzone.artistName}</td>
                <td class="col-lg-4" style="font-weight: normal">${canzone.title}</td>
                <td class="col-lg-4" style="font-weight: normal"><div><input id="input-id" type="number" class="rating" min=0 max=5 step=1 data-size="xs" data-rtl="false"></div></td>
            </tr>
        </form>
    </c:forEach>
    

    您必须在指定星号后决定如何提交表单,例如 onchange() 或其他。这不是很好的选择,但只是我所知道的..

    【讨论】:

    • 然后把表格放到标签外面,更新了我的答案,看看
    • 它以某种方式告诉我它看不到 结束标签...pastebin.com/VF42RDbA
    • 在您发布的有问题的代码中,您没有关闭 标记。您将 结束为 而不是 。也不要忘记在
    • 之后放置 关闭标签 .. 我认为您没有正确关闭标签..
  • 我关闭了第14行的tr!
  • 哪里是 ,我在 pastebin 代码中找不到 for Each 的结束标记
  • 猜你喜欢
    相关资源
    最近更新 更多
    热门标签