【问题标题】:If condition is not working for all td values?如果条件不适用于所有 td 值?
【发布时间】:2020-11-27 16:22:58
【问题描述】:

我正在使用 html 表,当我选中复选框时,我想比较每个 tr 中的 td 值,此条件适用于从 2nd tr 到 1st tr 的条件不起作用。
HTML 代码 -

<form role="form" name="conForm" id="conForm">
                    <span id="error" class="text-danger"></span>
                    <div class="table-responsive">
                        <table id="myPTable" class="table table-xss table-hover table-bordered">
                        <thead>
                        <tr>
                        <th><input class="checkall" type="checkbox" name="productcheckbox"> All</</th>
                        <th class="control-label paddingtop">SI No</th>
                        <th class="control-label paddingtop">Products</th>
                        <th class="control-label paddingtop">Pending Qty</th>
                        <th class="control-label paddingtop">Quantity</th>
                        <th class="control-label paddingtop">Amount</th>
                        </tr>
                        </thead>
                        <tbody>
                        <tr>
                        <td><input class="checkbox checkproduct" type="checkbox" name="check"></td>
                        <td>1</td>
                        <td>HMIS HMIS HMIS HMIS</td>
                        <td class="availableQty">10</td>
                        <td><input  type="number" name="select[]" class="enterQty" value="10" style="width:50px;"></td>
                        <td>3000</td>
                        </tr>
                        <tr>
                        <td><input class="checkbox checkproduct" type="checkbox" name="check"></td>
                        <td>2</td>
                        <td>ERP</td>
                        <td class="availableQty">1</td>
                        <td><input  type="number" name="select[]" class="enterQty" value="1" style="width:50px;" ></td>
                        <td>9000</td>
                        </tr>
                        <tr>
                        <td><input class="checkbox checkproduct" type="checkbox" name="check"></td>
                        <td>3</td>
                        <td>Inventory</td>
                        <td class="availableQty">10</td>
                        <td><input  type="number" name="select[]" class="enterQty" value="10" style="width:50px;"></td>
                        <td>13000</td>
                        </tr>
                        <tr><td><button class="btn-info">Save</button></td></tr>
                        </tbody>
            </table>
            </form>

这里是jquery代码-

$(".btn-info").on("click",function(){
               
                var check = $('.checkproduct');              
                  if(check.is(':checked')){
                      
                      var pending="";
                      $('#myPTable tr').each(function() {
                        pending += $(this).find(".availableQty").html();    
                        console.log(pending);
                      });
                
                      var enterqty ="";
                      $('#myPTable tr').each(function() {
                        enterqty += $(this).find(".enterQty").val();
                        console.log(enterqty);
                      });

                      if(enterqty > pending){
                          $("#error").html("<p>Quantity must be less than or equal to Pending Qty</p>");                          
                      }else if(enterqty== 0){
                          $("#error").html("<p>you have checked the product please enter quantity</p>");
                        }
                              else{                          
                        var checked = $('.checkproduct:checked').size();
                      }

我写的 jquery 只适用于第一行,但它需要适用于将要检查的每项工作。现在只有你们可以帮助我,在此先感谢。

【问题讨论】:

  • 你粘贴的代码中有=+,你的意思是+=吗?
  • @GhassenLouhaichi 抱歉拼写错误 +=。是对的。
  • 这是否可以解决问题,或者即使进行了更改,问题仍然存在?
  • @GhassenLouhaichi (enterqty== 0) 这个条件不起作用
  • Basanagouda Patil,我认为问题在于您将enterqty 声明为字符串,然后您尝试对其进行数学运算,就好像它是一个数字一样。

标签: javascript html jquery if-statement conditional-statements


【解决方案1】:

我对你的代码做了一些修改,但我相信这就是你想要的。

$(".btn-info").on("click", function() {
  var pending =0;
  var enterqty=0;
  $('#myPTable .checkproduct:checked').each(function() {
    pending += (+$(this).closest('tr').find(".availableQty").text());
    enterqty += (+$(this).closest('tr').find(".enterQty").val());
  });

  if (enterqty > pending) {
    $("#error").html("<p>Quantity must be less than or equal to Pending Qty</p>");
  } else if (enterqty == 0) {
    $("#error").html("<p>you have checked the product please enter quantity</p>");
  } else {
    //var checked = $('.checkproduct:checked').size();
  }
});

演示

$(".btn-info").on("click", function() {
  var pending =0;
  var enterqty=0;
  $('#myPTable .checkproduct:checked').each(function() {
    pending += (+$(this).closest('tr').find(".availableQty").text());
    enterqty += (+$(this).closest('tr').find(".enterQty").val());
  });

  if (enterqty > pending) {
    $("#error").html("<p>Quantity must be less than or equal to Pending Qty</p>");
  } else if (enterqty == 0) {
    $("#error").html("<p>you have checked the product please enter quantity</p>");
  } else {
    //var checked = $('.checkproduct:checked').size();
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form role="form" name="conForm" id="conForm">
  <span id="error" class="text-danger"></span>
  <div class="table-responsive">
    <table id="myPTable" class="table table-xss table-hover table-bordered">
      <thead>
        <tr>
          <th><input class="checkall" type="checkbox" name="productcheckbox"> All</</th>
            <th class="control-label paddingtop">SI No</th>
            <th class="control-label paddingtop">Products</th>
            <th class="control-label paddingtop">Pending Qty</th>
            <th class="control-label paddingtop">Quantity</th>
            <th class="control-label paddingtop">Amount</th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <td><input class="checkbox checkproduct" type="checkbox" name="check"></td>
          <td>1</td>
          <td>HMIS HMIS HMIS HMIS</td>
          <td class="availableQty">10</td>
          <td><input type="number" name="select[]" class="enterQty" value="10" style="width:50px;"></td>
          <td>3000</td>
        </tr>
        <tr>
          <td><input class="checkbox checkproduct" type="checkbox" name="check"></td>
          <td>2</td>
          <td>ERP</td>
          <td class="availableQty">1</td>
          <td><input type="number" name="select[]" class="enterQty" value="1" style="width:50px;"></td>
          <td>9000</td>
        </tr>
        <tr>
          <td><input class="checkbox checkproduct" type="checkbox" name="check"></td>
          <td>3</td>
          <td>Inventory</td>
          <td class="availableQty">10</td>
          <td><input type="number" name="select[]" class="enterQty" value="12" style="width:50px;"></td>
          <td>13000</td>
        </tr>
        <tr>
          <td><button type="button" class="btn-info">Save</button></td>
        </tr>
      </tbody>
    </table>

【讨论】:

  • 如果我检查所有其他 if (enterqty == 0) 条件不起作用。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-06-12
  • 2021-03-31
  • 1970-01-01
  • 2021-11-27
  • 1970-01-01
  • 2021-09-16
相关资源
最近更新 更多