简体   繁体   English

使用 Ajax 数据更新 html 模板

[英]Update html template with Ajax data

I'd like to update my html template with the data I have retrieved through an AJAX call.我想用我通过AJAX调用检索到的数据更新我的html模板。

I can fetch the data and define variables in JS .我可以获取数据并在JS定义变量。 However, how do I get the value from JS to Html , to update the value within the Html "pill"?但是,如何从JS获取值到Html ,以更新Html “药丸”中的值?

<span class="badge badge-primary badge-pill">0</span>

ie change the value from 0 to the value that is defined in var CarPrice = data.carPrice ;即将值从 0 更改为var CarPrice = data.carPrice定义的值; ? ?

JS: JS:

$(function () {
   var getProduct) = function () {
      var btn = $(this);
      $.ajax({
         url: btn.attr("data-url"),
         type: 'get',
         dataType: 'json',
         success: function (data){
            var CarMake = data.carMake
            var CarPrice = data.carPrice
            $("#CarMake").val(CarMake);
            $("#CarPrice").val(CarPrice);
         }, error: function(error){
         console.log(error)
         console.log("error")
         }
      });
   };

   $(".js-get-product").click(getProduct);
});

HTML (django): HTML (django):

    <div class="container">
        <h3>Key Metrics</h3>
        <div class="row">
            <div class="col-sm-4">
                <div class="card text-dark bg-light">
                    <div class="card-header bg-dark text-center text-light"><h4>CARS</h4></div>
                    <div class="card-body ">
                        <h5 class="card-title">Car Details/h5>
                        <!--Starting list group here -->
                        <div class="list-group">
                            <a class="list-group-item d-flex justify-content-between align-items-center list-group-item-action list-group-item-primary">
                                Car Make
                                <span class="badge badge-primary badge-pill">0</span>
                            </a>
                            <a class="list-group-item d-flex justify-content-between align-items-center list-group-item-action list-group-item-danger">
                                Car Price
                                <span class="badge badge-primary badge-pill">0</span>
                            </a>
                        </div>
                        <!--Ends here -->
                    </div>
                    <div class="card-footer bg-secondary border-danger text-right">
                        <a
                            class="btn btn-success btn-sm js-get-product"
                            data-url="{% url 'api-product' object.product_uid %}">Get Details</a>
                    </div>
                </div>
            </div>
        </div>
    </div>

Have you tried jQuery's text() method?你试过 jQuery 的text()方法吗? I update the 0 value by ids.我通过 ids 更新0值。 Your CarMake and CarPrice not assigned in span elements.您的 CarMake 和 CarPrice 未在 span 元素中分配。

like this:像这样:

<a class="list-group-item d-flex justify-content-between align-items-center list-group-item-action list-group-item-primary">
    Car Make
    <span id="CarMake" class="badge badge-primary badge-pill">0</span>
</a>
<a class="list-group-item d-flex justify-content-between align-items-center list-group-item-action list-group-item-danger">
    Car Price
    <span id="CarPrice" class="badge badge-primary badge-pill">0</span>
</a>
$(function () {
   var getProduct) = function () {
      var btn = $(this);
      $.ajax({
         url: btn.attr("data-url"),
         type: 'get',
         dataType: 'json',
         success: function (data){
            var CarMake = data.carMake
            var CarPrice = data.carPrice
            $("#CarMake").text(CarMake);
            $("#CarPrice").text(CarPrice);
         }, error: function(error){
         console.log(error)
         console.log("error")
         }
      });
   };

   $(".js-get-product").click(getProduct);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM