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>
添加回答
举报