我正在尝试使用 jQuery 比较 html 表中的两个值,并在数量 > 库存时提醒用户。
我想在当前行中选择值:
JS
$('.item_quantity').change(function(){
var $this=$(this);
var quantity=$this.val();
// var inventory=$this.next('.item_inventory, td').text();
var inventory=$(this).next('td, .item_inventory').text();
console.log('quantity: ', quantity, 'inventory: ', inventory);
if (quantity > inventory) {
alert('Your order quantity ' + quantity + ' is greater than ' + inventory + ' inventory');
}
});
});
HTML
<table >
<tr>
<td><form method=""><input class="item_quantity" type="text" data-prodid="249" value=""/></form></td>
<td class="item_inventory">10</td>
<td>Product 1</td>
</tr>
<tr>
<td><form method=""><input class="item_quantity" type="text" data-prodid="2" value=""/></form></td>
<td class="item_inventory">7</td>
<td>Product 2</td>
</tr>
</table>