[英]the “closest()” method in jquery wont work
所以我有這個HTML代碼和jQuery代碼。 當我按下帶有類名“ fa-products”的桌行“ produkter”按鈕時,我想找到與您選擇單擊的按鈕在同一桌行中的隱藏字段輸入(每個桌行都有一個隱藏字段輸入, “產品按鈕”)。 然后我想將隱藏字段的值保存在變量中,那就是有人可以幫助我嗎? 當我“ console.log(discountId);” 它回應不服氣
<div class="eastSide row top-buffer col-xs-8" style="overflow:scroll; height:250px;">
<table style="width:100%">
<tr>
<th>Code</th>
<th>Aktiv</th>
<th>Skapad</th>
<th></th>
</tr>
@foreach (var discount in Model.DiscountList)
{
<tr>
<td><input name="codeTextBox" id="codeTextBox" value="@discount.Code" maxlength="18" /></td>
<td><input type="checkbox" id="activeCheckBox" name="activeCheckBox" checked="@discount.Active" /></td>
<td><input type="datetime" value="@discount.CreatedDate" readonly /></td>
<td>
<input type="button" value="Radera" class="fa fa-remove" data-url="@Url.Action("DeleteDiscountCode","Discount",new { id= discount.Id})" />
<input type="button" value="Uppdatera" class="fa fa-update" data-url="@Url.Action("UpdateDiscount","Discount")" />
<input type="button" value="Produkter" class="fa fa-products" id="@discount.Id" data-url="@Url.Action("chooseProductsForDiscountCode","Discount")" />
</td>
<td><input id="id" type="hidden" value="@discount.Id" /></td>
</tr>
}
</table>
</div>
<script>
$(".fa-products").on("click", function (e) {
var discountId = $(event.target).closest('input[type="hidden"]').val();
console.log(discountId);
});
</script>
這將不起作用,因為隱藏的輸入不是已注冊元素的父級。
也許這可以解決您的問題: $(event.target).closest('tr').find('input[type="hidden"]').val();
您需要通過closest
搜索公共父元素,然后在結果內部查找input
:
$(".fa-products").on("click", function (e) {
var discountId = $(event.target).closest('tr').find('input[type="hidden"]').val();
console.log(discountId);
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.