2013-07-03 57 views
0

我有一個wordpress頁面,其中的產品顯示與Ajax添加到購物車操作。我想在點擊按鈕時獲得名稱=數量的輸入值。我想用jQuery prev()函數做這件事,因爲有很多其他輸入具有相同的屬性。那我該怎麼做?我有用jquery得到以前的元素值

jQuery(document).ready(function(e){ 
    e(document).on("click",".add_to_cart_button", 
      function(){ 
      var t=e(this); 
})}) 



<form action="/shop/?add-to-cart=1732" class="cart" method="post" enctype="multipart/form-data"> 
    <div class="quantity buttons_added"> 
     <input type="button" value="-" class="minus"> 
     <input type="number" step="1" name="quantity" value="1" title="Qty" class="input-text qty text"> 
     <input type="button" value="+" class="plus"> 
    </div> 
    <button type="submit" data-product_id="1732" data-product_sku="menu-aug-02" data-quantity="1" class="add_to_cart_button button product_type_simple">Add to cart</button></form> 
+0

哪個元素對你有一個參考? –

回答

1

試試這個

$('input[type=submit]').click(function() { 
    ... 
    ... 

    var prevInput = $(this).prev().children('input[name=quantity]'); // This is what you need 


    // Try with this 
    var requiredInput; 
    $(this).prev().children().each(function() { 
     if($(this).attr('name') != null) 
     { 
      requiredInput = $(this); 
     } 
    }); 

    ... 
    ... 
}); 

API
prev()
children()

+0

是的,我覺得這樣的東西!請參閱編輯! – MJQ

+0

你需要'提交'或'加'的點擊事件 - 我想加上只是爲了擴大一些細節?是嗎? – asifsid88

+0

我需要提交。 – MJQ

1

嘗試像

$('.plus').on('click',function(){ 
    var qty = $('input[name="quantity"]').val(); 
    alert(qty); 
}); 

您也可以與.before()嘗試像

$('.plus').on('click',function(){ 
    var qty = $(this).before('input[name="quantity"]').val(); 
    alert(qty); 
}); 

.prev()

$('.plus').on('click',function(){ 
    var qty = $(this).prev('input[name="quantity"]').val(); 
    alert(qty); 
}); 

如果你想它提交嘗試像

$('.cart').on('submit',function(e){ 
    e.preventDefault(); 
    var qty = $(this).prev('input[name="quantity"]').val(); 
    alert(qty); 
}); 
+0

看到我的第三個答案.. – Gautam3164

相關問題