WooCommerce添加到购物车如果数量增加,使用jQuery按钮文本更改

时间:2017-07-11 11:16:07

标签: javascript php jquery wordpress woocommerce

WooCommerce添加到购物车如果数量增加,则使用jQuery更改按钮文本。

    jQuery(document).on("change",'.minus', function() { 
  var productquantity = jQuery('.qty').attr('value');
    if  (productquantity == 15) {
          jQuery('.single-product').each(function() {
        jQuery(".single_add_to_cart_button").text("REQUEST A QUOTE");
    });
      }
})

选择按钮时,值永远不会改变:S

用于按钮和选择器的HTML

<div class="quantity buttons_added">
    <input type="button" value="-" class="minus button is-form"><input type="number" step="1" min="1" max="9999" name="quantity" value="1" title="Qty" class="input-text qty text" size="4" pattern="[0-9]*" inputmode="numeric"><input type="button" value="+" class="plus button is-form"></div>

<button type="submit" name="add-to-cart" value="1520" class="single_add_to_cart_button button alt" style="display: block !important">Add to Cart</button>

jsFiddle

2 个答案:

答案 0 :(得分:0)

&#13;
&#13;
$(document).ready(function ()
{
  $('body').on("click",'.minus',function ()
  {
      var qtyval = $('.qty').val();
      if(qtyval >= 0)
      {
        var newQTY = parseInt(qtyval) - parseInt(1);
        if(newQTY >= 15)
        {
         $('.single_add_to_cart_button').text("Request a Quote");
        }
        if(newQTY < 15)
        {
         $('.single_add_to_cart_button').text("Add to Cart");
        }
        $('.qty').val(newQTY);
      }
       
  });
  
   $('body').on("click",'.plus',function ()
   {
      var qtyval = $('.qty').val();
      var newQTY = parseInt(qtyval) + parseInt(1);
      if(newQTY >= 15)
      {
       $('.single_add_to_cart_button').text("Request a Quote");
      }
      $('.qty').val(newQTY);
   });
  
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.0/jquery.min.js"></script>

<div class="quantity buttons_added">
    <input type="button" value="-" class="minus button is-form"><input type="number" step="1" min="1" max="9999" name="quantity" value="1" title="Qty" 

class="input-text qty text" size="4" pattern="[0-9]*" inputmode="numeric"><input type="button" value="+" class="plus button is-form"></div>

<button type="submit" name="add-to-cart" value="1520" class="single_add_to_cart_button button alt" style="display: block !important">Add to Cart</button>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

jQuery(document).on("click",'.plus, .minus', function() { 
  var productquantity = jQuery('.qty').attr('value');
    if  (productquantity == 15) {
          jQuery('.single-product').each(function() {
        jQuery(".single_add_to_cart_button").text("REQUEST A QUOTE");
    });
      }  else if(productquantity != 15) {
          jQuery('.single-product').each(function() {
        jQuery(".single_add_to_cart_button").text("ADD TO CART NOW");
    });
      }
})

唯一的问题是改变onclick。