英文:
jQuery not setting textContent
问题
你可以尝试以下的JavaScript代码,来让你的按钮变得可用。你的HTML代码中已经有了一些数据属性(data-index),用于标识不同的产品。以下是针对加号、减号和更新按钮的JavaScript代码:
// 更新按钮
$(document).on('click', '.update-button', function (e) {
e.preventDefault();
var theproductid = $(this).data('index');
$.ajax({
type: 'POST',
url: '{% url "cart-update" %}',
data: {
product_id: theproductid,
product_quantity: $('#qty-show' + theproductid).text(),
csrfmiddlewaretoken: "{{csrf_token}}",
action: 'post'
},
success: function (json) {
// 重新加载页面
location.reload();
// 更新购物车数量
document.getElementById("cart-qty").textContent = json.qty;
// 更新小计
document.getElementById("total").textContent = json.total;
},
error: function (xhr, errmsg, err) {
// 错误处理
}
});
});
// 增加按钮
$(document).on('click', '.increase-button', function (e) {
var theproductid = $(this).data('index');
var counter = $('#qty-show' + theproductid);
var x = parseInt(counter.text());
// 阻止负数数量
if (x < 0) {
x = 0;
}
// 增加数量
counter.text(x + 1);
});
// 减少按钮
$(document).on('click', '.decrease-button', function (e) {
var theproductid = $(this).data('index');
var counter = $('#qty-show' + theproductid);
var x = parseInt(counter.text());
// 阻止负数数量
if (x < 0) {
x = 0;
}
// 减少数量,但不少于1
if (x > 1) {
counter.text(x - 1);
}
});
这些JavaScript代码应该能够让你的加号、减号和更新按钮正常工作。请确保你已经引入了jQuery库,并且你的Django视图(cart-update
)已正确处理这些按钮的操作。
英文:
I have a HTML file where I created my buttons and labels with for loop. (Django project)
Then, I tried to add some functionality using JS but my buttons don't work.
My failed part is +, - and update buttons with their functionality.
I don't know whether it is correct approach or not but I tried to differentiate my buttons assigning "button name"+{{product.id}} to their IDs.
<!-- start loop -->
{% for item in cart %}
<!-- assign product variable -->
{% with product=item.product %}
<br>
<div class="row mb-4 border product-item">
<div class="col-md-3 col-lg-2 order-md-first bg-light">
<img class="img-fluid mx-auto d-block" width="200px" alt="Responsive image" src="{{ product.image.url }}">
<!-- Product image -->
</div>
<div class="col-md-9 col-lg-10 ps-md-3 ps-lg-10">
<a href="{{ product.get_absolute_url }}" class="text-info text-decoration-none">
<h1 class="h5 pt-2"> {{ product.title }} </h1>
</a>
<div class="border">
<div class="col border-bottom">
<div class="row p-3">
<div class="col-6"> Product </div>
<div class="col-6 text-end"><span class="h6 fw-bold">$ {{ product.price|mul:item.qty}} </span></div>
</div>
</div>
<div class="col">
<div class="row p-3">
<div class="col-12">
<button type="button" id="qty-decrease-button{{product.id}}" data-index="{{product.id}}" class="btn btn-primary btn-sm increase-button"> - </button>
&nbsp;
<label id = "qty-show{{product.id}}" data-index="{{product.id}}" > {{item.qty}} </label>
&nbsp;
<button type="button" id="qty-increase-button{{product.id}}" data-index="{{product.id}}" class="btn btn-primary btn-sm decrease-button"> + </button>
<br> <br>
<button type="button" data-index="{{product.id}}" class="btn btn-primary btn-sm update-button"> Update </button>
&nbsp;
<button type="button" data-index="{{ product.id }}" class="btn btn-danger btn-sm delete-button"> Delete </button>
</div>
</div>
</div>
</div>
</div>
</div>
{% endwith %}
{% endfor %}
My JS codes at the end of this HTML code is:
// Update button
$(document).on('click', '.update-button', function (e) {
e.preventDefault();
var theproductid = $(this).data('index');
$.ajax({
type: 'POST',
url: '{% url "cart-update" %}',
data: {
product_id: $(this).data('index'),
// product_quantity: $('#select' + theproductid + ' option:selected').text(),
product_quantity: $('#qty-show' + theproductid).text(),
csrfmiddlewaretoken: "{{csrf_token}}",
action: 'post'
},
success: function (json) {
// reload page
location.reload();
// update cart
document.getElementById("cart-qty").textContent = json.qty
// update subtotal
document.getElementById("total").textContent = json.total
},
error: function (xhr, errmsg, err) {
}
});
})
</script>
<!-- Increment-Decrement buttons function -->
<script>
$(document).on('click', '.increase-button', function (e) {
var theproductid = $(this).data('index');
var counter = $('#qty-show' + theproductid);
x = counter.textContent;
// e.preventDefault();
counter.textContent = ++x;
})
$(document).on('click', '.decrease-button', function (e) {
var theproductid = $(this).data('index');
var counter = $('#qty-show' + theproductid);
x = counter.textContent;
// e.preventDefault();
if (x<=1)
{
counter.textContent = 1;
}
else
{
counter.textContent = --x;
}
})
</script>
Here you can see rendered HTML code:
(+/-/Update buttons does not work.)
If someone can help me for making these buttons functional, I would be appreciated!
答案1
得分: 1
counter
是一个 jQuery 集合,而不是一个原始的 DOM 元素。.textContent
是原始 DOM 元素的属性。
你可以要么访问原始的 DOM 元素,要么使用 jQuery API 来设置文本。可以这样做:
var counter = $('#qty-show' + theproductid).get(0);
或者这样做:
x = counter.text();
// ...
counter.text(x++);
英文:
counter
is a jQuery collection, not a raw DOM element. .textContent
is a property of raw DOM elements.
You can either access the raw DOM element, or use the jQuery API to stet text. Either do
var counter = $('#qty-show' + theproductid).get(0);
or do
x = counter.text();
...
counter.text(x++);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论