关于选中上一个表tr的值的一点表白。
我想获取上一个tr输入值的值,但它总是返回下一个tr值。我们正在选中其他元素更改事件的复选框。
我的代码如下:
/*
Here i want to get value of tr instead of click because
we have many conditions and I can't explain them all here
*/
$('body').on('change', '.change_product_price', function() {
var test1 = $(this).parent().parent().closest('tr').find('.chkIt').val();
console.log(test1)
});
$('.chkIt').click(function() {
if ($(this).is(':checked')) {
$(this).parent().parent().find('.change_product_price').val('1');
$(this).parent().parent().find('.change_product_price').change();
} else {
$(this).parent().parent().find('.change_product_price').val('0');
$(this).parent().parent().find('.change_product_price').change();
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table>
<tr class="product_row">
<td class="small-check SerialNumber"><input class="chkIt" type="checkbox" name="products[product_id][]" value="76" qty="0" product_weight="0"></td>
<td data-th="Product Name" class="fourth_i1">8 PACK SPECIALIST EMBOSSED SILVER FOIL 500 SHEET BOX 12CM X 20CM</td>
<td data-th="Part Number" class="grid-text">EMBOSS-Pre-Cut x 8 Pack</td>
<td class="small-check MarkGift"><input class="chkItg" type="checkbox" name="gift[product_id][]" value="78"></td>
<td data-th="Quantity" class="grid-text">
<select name="products[quantity][78][]" class="change_product_price">
<option>0</option>
<option>1</option>
<option>2</option>
<option>3</option>
</select>
<input type="hidden" class="promo_min_quantity" name="promo_min_quantity" value="">
<input type="hidden" class="promo_free_units" name="promo_free_units" value="0">
</td>
</tr>
<tr class="product_row">
<td class="small-check SerialNumber"><input class="chkIt" type="checkbox" name="products[product_id][]" value="78" qty="0" product_weight="0"></td>
<td data-th="Product Name" class="fourth_i1">8 PACK SPECIALIST EMBOSSED SILVER FOIL 500 SHEET BOX 12CM X 20CM</td>
<td data-th="Part Number" class="grid-text">EMBOSS-Pre-Cut x 8 Pack</td>
<td class="small-check MarkGift"><input class="chkItg" type="checkbox" name="gift[product_id][]" value="78"></td>
<td data-th="Quantity" class="grid-text">
<select name="products[quantity][78][]" class="change_product_price">
<option>0</option>
<option>1</option>
<option>2</option>
<option>3</option>
</select>
<input type="hidden" class="promo_min_quantity" name="promo_min_quantity" value="">
<input type="hidden" class="promo_free_units" name="promo_free_units" value="0">
</td>
</tr>
<tr class="product_row">
<td class="small-check SerialNumber"><input class="chkIt" type="checkbox" name="products[product_id][]" value="125" qty="0" product_weight="0"></td>
<td data-th="Product Name" class="fourth_i1">8 PACK SPECIALIST EMBOSSED SILVER FOIL 500 SHEET BOX 12CM X 20CM</td>
<td data-th="Part Number" class="grid-text">EMBOSS-Pre-Cut x 8 Pack</td>
<td class="small-check MarkGift"><input class="chkItg" type="checkbox" name="gift[product_id][]" value="125"></td>
<td data-th="Quantity" class="grid-text">
<select name="products[quantity][125][]" class="change_product_price">
<option>0</option>
<option>1</option>
<option>2</option>
<option>3</option>
</select>
<input type="hidden" class="promo_min_quantity" name="promo_min_quantity" value="">
<input type="hidden" class="promo_free_units" name="promo_free_units" value="0">
</td>
</tr>
</table>
这是我目前已勾选的每个tr输入76, 78 and 125
的3个值
问题是我已经检查了最后的$ 227.60复选框,但返回值为$ 174.40