1 回答

TA貢獻1818條經驗 獲得超3個贊
您不能擁有相同的多個元素ids,而是使用class選擇器。然后,只需使用提交數量值$(this).val()和庫存值.closest('tr').find('.stock')..,然后簡單地比較這些值。
演示代碼:
$('.submitQ').on("blur", function() {
//get value of submit qnty
let submit = $(this).val();
//get stock
let quantity = parseInt($(this).closest('tr').find('.stock').val());
if (submit > quantity) {
alert('Not enough goods!')
$(this).focus(); //show focus
}
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table>
<thead>
<th>No</th>
<th>Name</th>
<th>Brand</th>
<th>Requested Quantity</th>
<th>Submit Quantity</th>
<th>Stock</th>
</thead>
<tbody>
<tr>
<td>1</td>
<td>
<input type="text" name="[@i].GoodsName" readonly="readonly" asp-for="@item.GoodsName" value="something" class="form-control" />
</td>
<td>
<input type="text" name="[@i].BrandName" readonly="readonly" asp-for="@item.BrandName" class="form-control" />
</td>
<td>
<input type="text" name="[@i].Quantity" readonly="readonly" asp-for="@item.Quantity" class="form-control" />
</td>
<td>
<!--use class-->
<input type="number" class="submitQ" class="form-control" />
</td>
<td>
<input type="text" name="stock" value="8" class="stock" readonly="readonly" class="form-control" />
</td>
</tr>
<tr>
<td>2</td>
<td>
<input type="text" name="[@i].GoodsName" readonly="readonly" asp-for="@item.GoodsName" value="something" class="form-control" />
</td>
<td>
<input type="text" name="[@i].BrandName" readonly="readonly" asp-for="@item.BrandName" class="form-control" />
</td>
<td>
<input type="text" name="[@i].Quantity" readonly="readonly" asp-for="@item.Quantity" class="form-control" />
</td>
<td>
<input type="number" class="submitQ" class="form-control" />
</td>
<td>
<input type="text" name="stock" value="5" class="stock" readonly="readonly" class="form-control" />
</td>
</tr>
</tbody>
</table>
添加回答
舉報