[英]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.