亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在 Asp.net core 2.2 中使用 jQuery 比較動態表中的兩個值

在 Asp.net core 2.2 中使用 jQuery 比較動態表中的兩個值

Cats萌萌 2023-10-14 18:19:23
我有以下動態表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            }        })
查看完整描述

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>


查看完整回答
反對 回復 2023-10-14
  • 1 回答
  • 0 關注
  • 137 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號