使用onclick事件

时间:2018-04-13 11:50:15

标签: javascript ajax spring spring-boot thymeleaf

我使用的是Thymeleaf和Sprin Boot我的页面中有一个这样的对象:

然后我想在onc​​lick事件后更改对象的内容:我选择了一个日期,在我确认后,我向服务器发送请求,我必须在对象内显示答案(pdf) / p>

我该怎么做?

这是整个页面:

    <div class="row">
        <div class="col">
            <h5 style="color:red;">Seleziona data</h5>
            <div class="input-group input-group-prepend col-4">
                <span class="input-group-text far fa-calendar-alt"></span>
                <input type="text" class="form-control date" data-provide="datepicker" id="dataProgrammazione" ></input>
            </div>
            <button type="button" class="btn btn-primary" th:text="#{conferma}" onclick="cambiaData();"></button>
        </div>
    </div>
    <div class="row">
        <object class="col" th:data="@{/programmazione/pdf?dataProva=16/04/2018}" type="application/pdf" height="800" width="800" id="pdf"></object>
    </div>

    <script type="text/javascript">

        function cambiaData(){
            $.ajax({
                type: 'GET',  
                url: MY_URL+$("dataProgrammazione").val(),
                success: function(results) {
                    alert(results);
                    $('#pdf').data(results);
                }
            });
        }

        $(function(){
            $(".date").datepicker({
                todayBtn: "linked",
                language: "it",
                todayHighlight: true
            });
        });
    </script>

1 个答案:

答案 0 :(得分:1)

您可以执行以下操作:

<script type="text/javascript" th:inline="javascript">
    /*<![CDATA[*/
        function cambiaData(){
            $('#pdf').attr("data", [[ @{'NEW_URL_WITH_NEW_DATA}]]+$("#data").val());
        }

        $(function(){
            $(".date").datepicker({
                todayBtn: "linked",
                language: "it",
                todayHighlight: true
            });

        });
    /*]]>*/
    </script>

<div class="row">
        <h5 style="color:red;">Seleziona data</h5>
        <div class="input-group input-group-prepend col-2">
            <span class="input-group-text far fa-calendar-alt"></span>
            <input type="text" class="form-control date" data-provide="datepicker" id="data" th:value="${#dates.format(#dates.createNow(), 'dd/MM/yyyy')}"></input>
        </div>
        <button type="button" class="btn btn-primary" th:text="#{conferma}" onclick="cambiaData();"></button>
</div>
<div class="row">
    <object class="col" th:data="@{URL}" type="application/pdf" height="800" width="800" id="pdf"></object>
</div>