jQuery没有设置textContent

huangapple go评论72阅读模式
英文:

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.

 &lt;!-- start loop --&gt;
{% for item in cart %}
&lt;!-- assign product variable --&gt;
{% with product=item.product %}
&lt;br&gt;
&lt;div class=&quot;row mb-4 border product-item&quot;&gt;
&lt;div class=&quot;col-md-3 col-lg-2 order-md-first bg-light&quot;&gt;
&lt;img class=&quot;img-fluid mx-auto d-block&quot; width=&quot;200px&quot; alt=&quot;Responsive image&quot; src=&quot;{{ product.image.url }}&quot;&gt;
&lt;!-- Product image --&gt;
&lt;/div&gt;
&lt;div class=&quot;col-md-9 col-lg-10 ps-md-3 ps-lg-10&quot;&gt;
&lt;a href=&quot;{{ product.get_absolute_url }}&quot; class=&quot;text-info text-decoration-none&quot;&gt;
&lt;h1 class=&quot;h5 pt-2&quot;&gt; {{ product.title }} &lt;/h1&gt;
&lt;/a&gt;
&lt;div class=&quot;border&quot;&gt;
&lt;div class=&quot;col border-bottom&quot;&gt;
&lt;div class=&quot;row p-3&quot;&gt;
&lt;div class=&quot;col-6&quot;&gt; Product &lt;/div&gt;
&lt;div class=&quot;col-6 text-end&quot;&gt;&lt;span class=&quot;h6 fw-bold&quot;&gt;$ {{ product.price|mul:item.qty}} &lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;col&quot;&gt;
&lt;div class=&quot;row p-3&quot;&gt;
&lt;div class=&quot;col-12&quot;&gt;
&lt;button type=&quot;button&quot; id=&quot;qty-decrease-button{{product.id}}&quot; data-index=&quot;{{product.id}}&quot; class=&quot;btn btn-primary btn-sm increase-button&quot;&gt; - &lt;/button&gt;
&amp;nbsp;
&lt;label id = &quot;qty-show{{product.id}}&quot; data-index=&quot;{{product.id}}&quot; &gt; {{item.qty}} &lt;/label&gt;
&amp;nbsp;
&lt;button type=&quot;button&quot; id=&quot;qty-increase-button{{product.id}}&quot; data-index=&quot;{{product.id}}&quot; class=&quot;btn btn-primary btn-sm decrease-button&quot;&gt; + &lt;/button&gt;
&lt;br&gt; &lt;br&gt;
&lt;button type=&quot;button&quot; data-index=&quot;{{product.id}}&quot; class=&quot;btn btn-primary btn-sm update-button&quot;&gt; Update &lt;/button&gt;
&amp;nbsp;
&lt;button type=&quot;button&quot; data-index=&quot;{{ product.id }}&quot; class=&quot;btn btn-danger btn-sm delete-button&quot;&gt; Delete &lt;/button&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
{% endwith %}
{% endfor %}

My JS codes at the end of this HTML code is:

     // Update button
$(document).on(&#39;click&#39;, &#39;.update-button&#39;, function (e) {
e.preventDefault();
var theproductid = $(this).data(&#39;index&#39;);
$.ajax({
type: &#39;POST&#39;,
url: &#39;{% url &quot;cart-update&quot; %}&#39;,
data: {
product_id: $(this).data(&#39;index&#39;),
// product_quantity: $(&#39;#select&#39; + theproductid + &#39; option:selected&#39;).text(),
product_quantity:  $(&#39;#qty-show&#39; + theproductid).text(),
csrfmiddlewaretoken: &quot;{{csrf_token}}&quot;,
action: &#39;post&#39;
},
success: function (json) {
// reload page
location.reload();
// update cart
document.getElementById(&quot;cart-qty&quot;).textContent = json.qty
// update subtotal
document.getElementById(&quot;total&quot;).textContent = json.total
},
error: function (xhr, errmsg, err) {
}
});
})
&lt;/script&gt;
&lt;!-- Increment-Decrement buttons function --&gt;
&lt;script&gt;
$(document).on(&#39;click&#39;, &#39;.increase-button&#39;, function (e) {
var theproductid = $(this).data(&#39;index&#39;);
var counter = $(&#39;#qty-show&#39; + theproductid);
x = counter.textContent;
// e.preventDefault();
counter.textContent = ++x;
})
$(document).on(&#39;click&#39;, &#39;.decrease-button&#39;, function (e) {
var theproductid = $(this).data(&#39;index&#39;);
var counter = $(&#39;#qty-show&#39; + theproductid);
x = counter.textContent;
// e.preventDefault();
if (x&lt;=1)
{
counter.textContent = 1;
}
else
{
counter.textContent = --x;
}
})
&lt;/script&gt;

Here you can see rendered HTML code:
(+/-/Update buttons does not work.)

jQuery没有设置textContent

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 = $(&#39;#qty-show&#39; + theproductid).get(0);

or do

x = counter.text();
...
counter.text(x++);

huangapple
  • 本文由 发表于 2023年2月27日 05:01:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/75574988.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定