javascript - 在 Asp.net core 2.2 中使用 jQuery 比较动态表中的两个值
问题描述
我有以下动态表
我想将 submit Quantity
文本框和Stock
文本框的值一起比较,以检查Submit Quantity
值是否大于stock
所有行的值。
当submit Quantity
文本框失去焦点时,我想检查,如果Submit Quantity
值大于stock
,我想显示“库存中没有足够的商品”的警报,并且Submit Quantity
文本框必须再次获得焦点。
我的 HTML 和 C#
<tbody>
@{ var i = 0;}
@foreach (var item in Model)
{
<tr>
<td></td>
<td>
<input type="text" name="[@i].GoodsName" readonly="readonly" asp-for="@item.GoodsName" 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" onblur="compare()" id="submitQ" class="form-control" />
</td>
<td>
<input type="text" name="stock" id="stock" readonly="readonly" class="form-control" />
</td>
</tr>
}
我不知道该怎么做
任何帮助将不胜感激
提前致谢
编辑:
这是我为实现结果所做的,但它只适用于第一行Submit Quantity
文本框而不是第二行
function compare() {
$('#submitQ').each(function () {
let submit = $('#submitQ').val();
let quantity = $('#stock').val();
if (submit > quantity) {
alert('Not enough goods!')
$('#submitQ').select();
return false
}
})
解决方案
您不能使用相同的多个元素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>
推荐阅读
- c# - 使用 C# XmlElement 创建 XML 字符串
- bash - 如何使用 Bash 将 .flac 文件的目录(包括多个子目录)转换为 .mp3?
- html - 单击颜色时如何更改整个站点的背景颜色或文本颜色?角
- python - 如何将我的python默认为另一个python版本
- python - 如何根据列表元素的类型将列表转换为字符串
- r - 当条件表达式存储在数据框列中时,如何迭代运行 dplyr::case_when()?
- sql - SQL语句获取未排序的书名
- ms-office - Office 始终以匿名用户身份请求文件
- r - 需要帮助在 Mac 上安装 rJava
- r - Rearrange a dataframe into a matrix based on column values in R?