[英]select parent element class
My html code is as follows: 我的html代码如下:
<div id="cart-list">
<div class="col-xs-12 product-grid-item">
<span class="product-grid-id" style="display: none;">3</span>
<div class="product-info col-xs-12">
<div class="col-xs-8 product-metadata">
<div class="name">Name</div>
<div class="price">Price</div>
<div class="col-xs-6" id="delete">
<button type="submit" name="delete-btn" id="delete-btn">Delete</button>
</div>
</div>
</div>
</div>
<div class="col-xs-12 product-grid-item">
...
</div>
<div class="col-xs-12 product-grid-item">
...
</div>
<div class="col-xs-12 product-grid-item">
...
</div>
</div>
JQuery is : jQuery是:
$(".product-grid-item").on('click','#delete-btn',function(e) {
e.preventDefault();
var id = $(this).find(".product-grid-id").text();
deleteFromCart(id);
return false;
});
I want on delete button click, it should select the span element with class name product-grid-id and get the text in there, which is 3, in this case and pass it forward.. 我要单击删除按钮,它应该选择类名称为product-grid-id的span元素,并在其中获取文本(在这种情况下为3)并将其向前传递。
NOTE : There will be many product-grid-item div's in cart-list div. 注意 :购物车列表div中将有许多product-grid-item div。 I need to get the id of the div on which the delete button is clicked. 我需要获取单击删除按钮的div的ID。 this delete button will be in every product-grid-item 此删除按钮将出现在每个产品网格项目中
Your problem is that the span
element with the product-grid-id
class is not a child of your current button, and also not a direct parent of it, so what you actually need to do is find a relevant parent and then find the span.product-grid-id
element inside that parent 您的问题是,带有product-grid-id
类的span
元素不是当前按钮的子元素,也不是它的直接父元素,因此您实际要做的是找到一个相关的父元素,然后找到span.product-grid-id
该父级内部的span.product-grid-id
元素
To find a parent up the dom-tree you need to use the parents(SELECTOR)
function, and there you can use the find(SELECTOR)
function to get the element you are looking for. 要在dom树上找到父级,您需要使用parents(SELECTOR)
函数,然后可以在其中使用find(SELECTOR)
函数来获取所需的元素。
This is an example: (note that I commented-out the call to deleteFromCart
function). 这是一个示例:(请注意,我已注释掉对deleteFromCart
函数的调用)。
$(".product-grid-item").on('click','#delete-btn',function(e) { e.preventDefault(); var id = $(this).parents('.product-grid-item').find(".product-grid-id").text(); //deleteFromCart(id); console.log(id); return false; });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="cart-list"> <div class="col-xs-12 product-grid-item"> <span class="product-grid-id" style="display: none;">3</span> <div class="product-info col-xs-12"> <div class="col-xs-8 product-metadata"> <div class="name">Name</div> <div class="price">Price</div> <div class="col-xs-6" id="delete"> <button type="submit" name="delete-btn" id="delete-btn">Delete</button> </div> </div> </div> </div> <div class="col-xs-12 product-grid-item"> </div> <div class="col-xs-12 product-grid-item"> </div> <div class="col-xs-12 product-grid-item"> </div> </div>
You may select the closest: 您可以选择最接近的:
<div class="product-info col-xs-12">
and then the previous element <span class="product-grid-id" style="display: none;">3</span>
然后是上一个元素<span class="product-grid-id" style="display: none;">3</span>
Th snippet: 摘录:
$(".product-grid-item").on('click', '#delete-btn', function (e) { e.preventDefault(); var id = $(this).closest('div.product-info.col-xs-12').prev('span.product-grid-id').text(); console.log(id); // do your stuff });
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"> <script src="https://code.jquery.com/jquery-2.1.1.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> <div id="cart-list"> <div class="col-xs-12 product-grid-item"> <span class="product-grid-id" style="display: none;">3</span> <div class="product-info col-xs-12"> <div class="col-xs-8 product-metadata"> <div class="name">Name</div> <div class="price">Price</div> <div class="col-xs-6" id="delete"> <button type="submit" name="delete-btn" id="delete-btn">Delete</button> </div> </div> </div> </div> <div class="col-xs-12 product-grid-item"> ... </div> <div class="col-xs-12 product-grid-item"> ... </div> <div class="col-xs-12 product-grid-item"> ... </div> </div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.