javascript - Thymeleaf + Javascript 选择选项更改

标签 javascript thymeleaf jquery-select2

我想选择一个元素列表,基于选择我调用一个函数 api 给我一个数据列表,插入第二个选择。

调用正常,我尝试提醒结果,但我需要更改第二次选择的值,但我失败了。 我正在使用 select2

这是我的 THYMELEAF 页面:

   <form id="updateD" data-parsley-validate class="rounded px-2 py-2 mt-3" th:action="@{/ditta/upd}" th:method="POST" th:object="${ditta}">

<div class="row" th:if="*{codice==null or codice.isEmpty()}">
                    <div th:replace="~{/tags/input :: inputNorm(indirizzo, *{indirizzo}, text, true, '')}"></div>
                    <div th:replace="~{/tags/select :: selectValue(city, *{city}, ${cities}, false)}"></div>
                    <div th:replace="~{/tags/select :: selectValue(zip, *{zipCode}, ${zips}, false)}"></div>
                </div>
</form>     

JS代码:

<script type="text/javascript" th:inline="javascript">
    /*<![CDATA[*/
        var token = $('#_csrf').attr('content');
        var header = $('#_csrf_header').attr('content');

        $(function(){

            $('#city').change(function(){
                handleComune($('#city').val());
            });

            $('.sel2').select2({
                theme: "bootstrap"
            });
        });

        function handleComune(city){
            $.ajax({
                type: 'GET',
                 url:[[ @{'/api/extractZip?city='}]]+city,
                 beforeSend: function(xhr) {
                   xhr.setRequestHeader(header, token);
               },
               success : function(data) {
                   $('#zips').val(data);
                   alert(data);
               }
            });
        }

    /*]]>*/
    </script>

根据城市的选择,我提取了邮政编码列表。

选择的片段如下:

    <div th:fragment="selectValue(name, selected, values, required)" class="col form-group" th:switch="${required}">

        <label th:text="${required} ? #{${name}}+'*' : #{${name}}" th:for="${name}" class="control-label"></label>

<div class="col form-group">
            <select th:name="${name}" th:id="${name}" class="form-control sel2" th:required="${required}">
                <option value=""></option>
                <th:block th:each="v : ${values}">
                    <option th:value="${v}" th:text="${v}" th:selected="${selected} eq ${v}"></option>
                </th:block>
            </select>
        </div>

    </div>

$('#zips').val(data); 不变。 怎么了?

最佳答案

如果您从 Controller 返回 Thymeleaf 片段,那么您将获得一个 html 代码。因此,val() 将不起作用。您需要使用 append(data)。现在我没有看到您的 Controller 或元素 $('#zips') 所在的位置,所以我假设这就是您要发送的内容。

如果您要发送实体列表,则需要使用 $.each(data, function() {//Do something }); 将每个实体添加为选项您的选择。

关于javascript - Thymeleaf + Javascript 选择选项更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52985253/

相关文章:

javascript - angularJS 页面不渲染任何内容

javascript - 在 Json 中搜索过滤器

java - 如何从 Thymeleaf 调用对象的方法?

twitter-bootstrap - 使用 Bootstrap 和 Thymeleaf 从列表中动态创建选项卡

jquery - 指定 Select2 数据时出错

javascript - Jquery 旋转在点击时旋转图像

javascript - Uncaught ReferenceError : message is not defined?

gradle - Ratpack + Thymeleaf + ShadowJar - 解析模板时出错 "home",模板可能不存在或无法访问

jquery - 即使使用 CTRL 也限制 select2 选择

python - 在selenium中单击select2下拉列表时出现问题