使用jQuery获取先前的元素值 [英] get the previous element value with jquery
本文介绍了使用jQuery获取先前的元素值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个wordpress页面,上面用ajax显示产品添加到购物车中.我想在单击按钮时获取name = quantity的input的值.我想用jquery prev()函数做到这一点,因为还有许多其他输入都具有相同的属性.那我该怎么办呢?我有
I have a wordpress page on which products are shown with ajax add to cart action. I want to get the value of input with name=quantity when button is clicked. I want to do this with jquery prev() function as there are many other inputs with same attributes. So how can i do that? I have
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>
推荐答案
尝试一下
$('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()
这篇关于使用jQuery获取先前的元素值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文