简体   繁体   English

在向节点发送 ajax 请求而不重新加载页面后更新我在 ejs 中的视图

[英]Update my view in ejs after sending an ajax request to node without reloading page

I have a shopping cart that has a quantity field containing a plus button which the user can click to increase quantity.我有一个购物车,其中有一个数量字段,其中包含一个加号按钮,用户可以单击该按钮来增加数量。
I was looking for a way to update the DOM without reloading the page.我正在寻找一种无需重新加载页面即可更新 DOM 的方法。
I found out about AJAX requests today and I've managed to write the code in such a way that if the user clicks on the add(+) button, the unit price is added to the total Price and is displayed.我今天发现了有关 AJAX 请求的信息,并且我设法编写了这样的代码,即如果用户单击添加(+)按钮,单价将添加到总价格中并显示。
My issue is that this only works on the first try, but on subsequent clicks the UI is not updated.我的问题是这仅在第一次尝试时有效,但在随后的点击中,UI 不会更新。

Ajax script: Ajax 脚本:

$("#plus_btn").on("click", function (event) {
  event.preventDefault();
  event.stopPropagation();
  var onekg = document.getElementById("onekg").value;
  var totalPrice = document.getElementById("totalPrice").value;

  var data = {};
  data.onekg = onekg;
  data.totalPrice = totalPrice;

  $.ajax({
    url: "/update-shopping-cart",
    type: "POST",
    data: data,
  })
    .done(function (result) {
      updateDOM(result);
    })
    .fail(function (err) {
      console.log(err);
    });
});

var updateDOM = function (result) {
  var totalPrice_div = document.getElementById("price_span");

  totalPrice_div.innerHTML = result;
};

EJS VIEW EJS 视图

   <h1>$ 1000</h1>

   //Unit price of item
    <input type="hidden" name="onekg" id="onekg" value="1000">

 <button id="plus_btn" type="button" name="button">+</button>

 //Total price to be updated and displayed on the DOM
   <span id="price_span" >$ 1000</span>
     <input type="hidden" name="totalPrice" id="totalPrice" value="1000">


routes.js路由.js

router.route("/update-shopping-cart").post((req, res) => {
    console.log(req.body);
    let totalPrice = parseInt(req.body.totalPrice) + parseInt(req.body.onekg);
    res.status(200);
    res.render('includes/totalPrice', {
        subTotal: totalPrice
    });
    res.end();

});

includes/totalPrice.ejs - ejs template To update the DOM包含/totalPrice.ejs - ejs 模板更新 DOM

<span id="price_span">Ksh. <%=subTotal %></span>

I found out that it wasn't updating because I wasn't updating the input field containing the value as well includes/totalPrice.ejs我发现它没有更新,因为我没有更新包含该值的输入字段以及包括/totalPrice.ejs

<span id="price_span" class="price text-right">Ksh. <%=subTotal %></span>
<input type="hidden" name="totalPrice" id="totalPrice" value="<%=subTotal %>">

I then wrapped both the span and the input with a div然后我用 div 包装了跨度和输入

   <div id="total_div" class="total-price border">
      <span id="price_span" class="price text-right">Ksh. <%=cake.price %></span>
      <input type="hidden" name="totalPrice" id="totalPrice" value="<%=cake.price %>">
   </div>

Then finally in the ajax request function, I changed the updateDOM function from getting the id of the span to getting the id of that total_div然后最后在 ajax 请求 function 中,我将 updateDOM function 从获取 span 的 id 更改为获取total_div的 id

var updateDOM = function (result) {
  var totalPrice_div = document.getElementById("total_div");

  totalPrice_div.innerHTML = result;
};

:)) :))

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

相关问题 EJS-更新数据库中的数据而无需从EJS重新加载页面 - EJS - Updating data in my database without reloading the page from EJS AJAX:更新页面上的textarea而不重新加载 - AJAX: Update textarea on page without reloading Asp.net MVC使用ajax在15秒后更新网页而不重新加载网页 - Asp.net MVC update a web page without reloading the page after 15 seconds using ajax 从服务器接收ajax回调中的数据,而无需重新加载并更新当前页面。 Express 4和节点 - Receive data in ajax call back from server without reloading and update current page. Express 4 and Node 使用Ajax将表单数据发送到Node.js而不重新加载页面 - Sending form data to nodejs without reloading page using Ajax 将确认警报从Node / Express发送到浏览器,而无需重新加载页面 - Sending confirmation alerts from Node/Express to Browser without reloading page 如何在不重新加载整个页面的情况下使用ajax,jquery,javascript更新动态网页内容 - how to update my dynamic web content without reloading the whole page using ajax, jquery, javascript 如何在ajax请求后使用新数据重新呈现.ejs页面 - How to re render a .ejs page with new data after ajax request 停止发送ajax请求而不刷新页面 - Stop sending ajax request without page refresh 更改浏览器URL,而无需使用Ajax请求重新加载页面 - Change browser url without page reloading with ajax request
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM