WooCommerce global.js does not work after updating the shopping cart

I write personnal javascript in global.js
, and it work until I click on the WooCommerce button update_cart
.

After my function doesn’t work.

The function is for update notification display :

$('#nb_article').change(function() {
        if ($('#nb_article').text() = 0) {
            $('#nb_article').css("display", "none");
        } else {
            $('#nb_article').css("display", "block");
        }
    });

and it’s in balise jQuery(document).ready(function($) {}
.

Do you know what can I do ?

The update cart action is triggered on woocommerce/assets/js/frontend/cart.js
, look for update_cart: function() {

It makes a ajax call to the url of form and then update the .woocommerce div with the html returned by ajax call. So, it doesn’t work maybe because $(‘#nb_article’) is inside .woocommerce and the plugin will replace all html, losing the events. You should reapply the event handler after update.

Or in alternative, you can apply the event by delegation, in this way:

$('.woocommerce').on( 'change', '#nb_article', function() {
    var $nb_article = $(this);

    if ( $nb_article.text() == 0) {
        $nb_article.css("display", "none");
    } else {
        $nb_article.css("display", "block");
    }
});
Hello, buddy!稿源:Hello, buddy! (源链) | 关于 | 阅读提示

本站遵循[CC BY-NC-SA 4.0]。如您有版权、意见投诉等问题,请通过eMail联系我们处理。
酷辣虫 » 前端开发 » WooCommerce global.js does not work after updating the shopping cart

喜欢 (0)or分享给?

专业 x 专注 x 聚合 x 分享 CC BY-NC-SA 4.0

使用声明 | 英豪名录