【问题标题】:dynamically pass parameter in js inside dynamically created <div>在动态创建的 <div> 里面的 js 中动态传递参数
【发布时间】:2022-01-15 21:33:13
【问题描述】:

我有这个动态创建 div 的脚本。我想将参数传递给控制器​​,并能够在单击预订按钮后可视化预订表格。 使用 thymeleaf 我会这样做 th:href="@{/hotels/{id}/booking-form(id=*{id})}" 但是我不确定如何在动态创建的内部使用 thymeleaf分区。

所以我尝试使用 javascript,但当然,它不起作用,所以如果有人告诉我我做错了什么以及如何获取 id 并将其传递给控制器​​,我将不胜感激。

                    <script type="text/javascript" th:inline="javascript">

                    document.getElementById('showAll').addEventListener('click', showAll);


                    function showAll() {
                        fetch('http://localhost:8080/accommodations')
                            .then(response => {
                                return response.json()
                            })
                            .then((data) => {
                                let output = `<h2> Search Results </h2>`;
                                data.forEach(function (hotel) {
                                    output += `
                                /some divs here/
                             
                                      <!-- Book Now button -->
                                    <div class="d-flex justify-content-end mt-1">
                                        <div class="btn btn-primary text-uppercase" id="bookingBtn" data-hotel-id="' + hotel.id + '" >Book Now</div>
                                    </div>
                                </div>`;
                                });
                                document.getElementById('output').innerHTML = output;
                            })
                    }

                    function booking() {
                        let hotelId = $(this).data('hotel-id')
                        fetch('http://localhost:8080/hotels/' + hotelId + '/booking-form', {
                            method: 'POST'
                        })
                    }

                    document.getElementById('bookingBtn').addEventListener('click', booking);


                </script>

【问题讨论】:

    标签: javascript fetch-api


    【解决方案1】:

    当您使用template literals 创建字符串时,您可以通过${var} 这样的方式包含变量。所以在你的情况下你应该做data-hotel-id="${hotel.id}"

    data.forEach(function(hotel) {
          output += `
             /some divs here/
                                     
             <!-- Book Now button -->
             <div class="d-flex justify-content-end mt-1">
               <div class="btn btn-primary text-uppercase" id="bookingBtn" data-hotel-id="${hotel.id}" >Book Now</div>
               </div>
             </div>`;
        });
    

    【讨论】:

    • 谢谢你,我试过你的建议,但它仍然没有做任何事情。也许我的错误在其他地方。
    • 您是否检查了生成的元素是否包含 data-hotel-id 属性?您可以检查您的页面来检查这一点。在迭代时还要检查酒店是否包含 id 值。
    • 迭代时,每家酒店都有一个id,但是hotelId是未定义的
    • 尝试在您的预订功能中console.log($(this)) 以检查您是否可以访问该元素
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-09-27
    • 2017-03-13
    • 2020-02-22
    • 2017-09-04
    • 2023-03-06
    • 1970-01-01
    • 2013-05-08
    相关资源
    最近更新 更多