2016-05-31 26 views
-1

我是jquery的新手,我很樂意爲您提供幫助。我需要操縱一下eshop頁面。當點擊+/-或手動更改輸入值時,我需要更新最近的a鏈接的data-quantityjquery更新最近輸入變化時的數據量 -

我具有以下的代碼的一部分(內側某些產品說明):

<div class="small-product"> 

<div class="text-center"> 

    <div class="add-to-cart-button"> 
     <a href="/zeleny/kategorie-produktu/vareni/?removed_item=1&amp;add-to-cart=3854" data-quantity="1" rel="nofollow" data-product_id="3854" class="ajax_add_to_cart add_to_cart_button product_type_simple button alt-button small clearfix">Do košíku</a> 
    </div> 

    <div class="quantity"> 
    <input type="number" step="1" min="1" max="8" name="quantity" value="1" title="Množství" class="input-text qty text" size="4"> 
    </div> 

</div> 
<div class="small-product"> 

<div class="text-center"> 

    <div class="add-to-cart-button"> 
     <a href="/zeleny/kategorie-produktu/vareni/?removed_item=1&amp;add-to-cart=3854" data-quantity="1" rel="nofollow" data-product_id="3854" class="ajax_add_to_cart add_to_cart_button product_type_simple button alt-button small clearfix">Do košíku</a> 
    </div> 

    <div class="quantity"> 
    <input type="number" step="1" min="1" max="13" name="quantity" value="1" title="Množství" class="input-text qty text" size="4"> 
    </div> 


</div> 

這個jQuery增加+ - 按鈕和點擊時控制它的最低和最高值:

(function($) { 
    function createQTYButtons(target) { 
     // Quantity buttons 
     $(target).find('div.quantity:not(.buttons_added), td.quantity:not(.buttons_added)').addClass('buttons_added').append('<input type="button" value="+" class="plus" />').prepend('<input type="button" value="-" class="minus" />'); 
     // Target quantity inputs on product pages 
     $(target).find('input.qty:not(.product-quantity input.qty)').each(function() { 
      var min = parseFloat($(this).attr('min')); 
      if (min && min > 0 && parseFloat($(this).val()) < min) { 
       $(this).val(min); 
      } 
     }); 
     $(target).on('click', '.plus, .minus', function() { 
      // Get values 
      var $qty = $(this).closest('.quantity').find('.qty'), 
       currentVal = parseFloat($qty.val()), 
       max = parseFloat($qty.attr('max')), 
       min = parseFloat($qty.attr('min')), 
       step = $qty.attr('step'); 
      var $qty_cart = $(this).closest('.text-center').find('.addt_to_cart_button'), 
       qty_cart_val = parseFloat($qty_cart.data("quantity")); 
       console.log(qty_cart_val);   
      // Format values 
      if (!currentVal || currentVal === '' || currentVal === 'NaN') currentVal = 0; 
      if (max === '' || max === 'NaN') max = ''; 
      if (min === '' || min === 'NaN') min = 0; 
      if (step === 'any' || step === '' || step === undefined || parseFloat(step) === 'NaN') step = 1; 
      // Change the value 
      if ($(this).is('.plus')) { 
       if (max && (max == currentVal || currentVal > max)) { 
        $qty.val(max); 
       } else { 
        $qty.val(currentVal + parseFloat(step)); 
       } 
      } else { 
       if (min && (min == currentVal || currentVal < min)) { 
        $qty.val(min); 
       } else if (currentVal > 0) { 
        $qty.val(currentVal - parseFloat(step)); 
       } 
      } 
      // Trigger change event 
      $qty.trigger('change'); 
     }); 
    } 
    // jQuery plugin. 
    $.fn.addQty = function() { 
     return this.each(function(i, el) { 
      createQTYButtons(el); 
     }); 
    } 
})(jQuery);  
jQuery('.small-product').addQty(); 

這裏是小提琴: https://jsfiddle.net/kybernaut/8aam5861/

非常感謝

+1

大部分現代瀏覽器做這個用'型= number'自動。你爲什麼需要自己做這個? – Barmar

回答

0

這裏是如果這將是任何人有用的答案,我已經找到了自己的解決方案:

$('.qty').bind('change', function() { 
    $(this).closest('.text-center').find('a.ajax_add_to_cart').attr('data-quantity', $(this).val()); 
});