简体   繁体   中英

jQuery works in console but when I save it in my site it doesn't

Created this jQuery to add an additional price in my wordpress site:

jQuery(document).ready( function(){
    var totprice = jQuery(".order-total .woocommerce-Price-amount.amount").text().replace('$','').replace(',','');
    var leaseprice = (Number(totprice)) * (0.0198);
    jQuery(".woocommerce-checkout-review-order-table tbody").append("<tr class='lp-total'><td class='lp-text'>Estimated Commercial Lease Total: </td>" + "<td class='lp-price'>" + "$" + leaseprice.toFixed(1) + "</td>"+ "</tr>");
});

The html:

<table class="shop_table woocommerce-checkout-review-order-table">
    <thead>
        <tr>
            <th class="product-name">Product</th>
            <th class="product-total">Subtotal</th>
        </tr>
    </thead>
    <tbody>
        <tr class="cart_item">
            <td class="product-name"> Max-EV 20&nbsp; <strong class="product-quantity">×&nbsp;1</strong> </td>
            <td class="product-total"> <span class="woocommerce-Price-amount amount"><span class="woocommerce-Price-currencySymbol">$</span>30,950.00</span>
            </td>
        </tr>
    </tbody>
    <tfoot>
        <tr class="cart-subtotal">
            <th>Subtotal</th>
            <td><span class="woocommerce-Price-amount amount"><span class="woocommerce-Price-currencySymbol">$</span>30,950.00</span>
            </td>
        </tr>
        <tr class="order-total">
            <th>Total</th>
            <td><strong><span class="woocommerce-Price-amount amount"><span class="woocommerce-Price-currencySymbol">$</span>30,950.00</span></strong> </td>
        </tr>
    </tfoot>
</table>

It works just fine in console but as soon as I save it in the custom.js file and press refresh button to check if it is working or not, I see the html from jQuery for a second then it hides automatically.

Please note if I append the html anywhere else on the same page it works fine. But here I'm trying to append it inside a tag and when I do that this issue occurs.

Is something wrong in the jQuery code?

Did you include jQuery into your JS?

var script = document.createElement('script');
script.src = '//code.jquery.com/jquery-3.4.1.min.js';
script.type = 'text/javascript';
document.getElementsByTagName('head')[0].appendChild(script);

Also try to clear your cache.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM