【问题标题】:JavaScript + Thymeleaf - select onchangeJavaScript + Thymeleaf - 选择 onchange
【发布时间】:2015-06-01 17:39:01
【问题描述】:

我的 thymeleaf 上下文中有一个对象列表 services,我的页面上有以下内容:

<select id="inputService" name="idService" size="1">
    <option th:each="service : ${services}" th:text="${service.name}" th:value="${service.idService}"/>
</select>
<p id="selectedServiceLimits"></p>

services 中的每个对象都包含字段 minAmountmaxAmount。如何通过javascript将select中选定service的这两个字段打印到我的p元素中?以及如何打印文档准备好时选择的选项的这两个字段?

谢谢!

【问题讨论】:

    标签: java javascript jquery html thymeleaf


    【解决方案1】:
    <script th:inline="javascript">
                /*<![CDATA[*/
                function showLimits() {
                    var services = /*[[${services}]]*/ null;
                    var selectedIndex = $("#inputService option:selected").index();
                    var service = services[selectedIndex];
                    $("#amountLimits").text("Мин. сумма: " + service.amountMin + ", макс. сумма: " + service.amountMax);
                }
                $(function() { showLimits(); });
                /*]]>*/
    </script>
    
    <select id="inputService" name="idService" size="1" onChange="showLimits()">
    

    这就是解决办法

    【讨论】:

    • 这非常适合这个特定的用例。原始问题中的

      标记的 id 为“selectedServiceLimits”,因此如果您使用此解决方案,您应该将提供的 JavaScript 的“#amountLimits”更改为“#selectedServiceLimits”,以使用您预先存在的 html

    猜你喜欢
    • 2011-11-07
    • 1970-01-01
    • 2022-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多