如何使用Java脚本onfocusout在PHP while循环中动态求和多个输入记录

问题描述

我正在使用一种方法来计算此ID id="salenag<?PHP echo $a; ?>" 的值 使用PHP来自数据库的多个值,然后将值添加到输入字段中,所有所有输入字段的总和以id no sale_nag显示。 我为此使用了Javascript方法,但是没有用; 它如何正常工作,用于销售点库存不足。我想要一个解决方案。请帮帮我。

<input type='text' value='0' id='sale_nag'>
  <table>
    <?PHP
    $a =1;
    while ($pro_row = MysqLi_fetch_assoc($select_pro)) {
      $rm =  $pro_row['room'];
      $ra =  $pro_row['rack'];
      $sal =  $pro_row['sale_nag'];
      $ma = $pro_row['marka'];
      $cha = $pro_row['chalan'];


      $pro_query2="SELECT *,SUM(sale_nag) AS snag FROM stockout_details  WHERE  marka='$ma' and chalan='$cha' and room='$rm' and rack='$ra'
      ";
      $select_pro2 = MysqLi_query($con,$pro_query2);
      $pro_row2 = MysqLi_fetch_assoc($select_pro2);
      if ( $sal - $pro_row2['snag'] > 0) {

        ?>
        <div class="form_grid_2 d-none"  >
          <input name="inv[]" required class=""  value="<?PHP echo $inv_pro1 ; ?>"   type="text"/>
          <span class="label_intro ">inv #</span>
        </div>
        <div class="form_grid_2 d-none"  >
          <input name="purchi[]" required class=""  value="<?PHP echo $id ; ?> "   type="text"/>
          <span class="label_intro ">Purchi #</span>
        </div>
        <div class="form_grid_2 d-none"  >
          <input name="invstockin[]" required class="" readonly value="<?PHP echo $pro_row['stockoutinv'];?>"   type="text"/>
          <span class="label_intro ">Inv#</span>
        </div>
        <div class="form_grid_2"  >
          <input name="marka[]" required class=""  readonly value="<?PHP echo $ma = $pro_row['marka'];?>"   type="text"/>
          <span class="label_intro ">Marka #</span>
        </div>
        <div class="form_grid_2 "  >
          <input name="chalan[]" required class=" " readonly value="<?PHP echo $cha = $pro_row['chalan'];?>"   type="text"/>
          <span class="label_intro ">Chalan #</span>
        </div>

        <div class="form_grid_2 "  >
          <?PHP

          ?>
          <input name="nag[]"  class="" readonly value="<?PHP
          echo   $sal - $pro_row2['snag'] ;
          ?>"    type="text"/>
          <span class="label_intro ">NAG #</span>
        </div>

        <div class="form_grid_2 " >
          <input name="outnag[]"  id="salenag<?PHP echo $a; ?>" onfocusout="chachvalue(<?PHP echo $a; ?>);" class=""  value="0"    type="text"/>
          <span class="label_intro ">Nag stock-out #</span>
        </div>

        <div class="form_grid_2 "  >
          <input name="room[]" required class="" readonly value="<?PHP echo $pro_row['room'];?>"   type="text"/>
          <span class="label_intro ">Room #</span>
        </div>
        <div class="form_grid_2 "  >
          <input name="rack[]" required class="" readonly  value="<?PHP echo $pro_row['rack'];?>"   type="text"/>
          <span class="label_intro ">Rack #</span>
        </div>
        <script type="text/javascript">
        function  chachvalue(<?PHP echo $a;?>){

          var x1 = document.getElementById('salenag<?PHP echo $a ;?>').value;
          var x2 = document.getElementById('sale_nag').value;
          var result  = x1 + x2 ;
          document.getElementById('sale_nag').value=result;

        }

        </script>


        <?PHP
  $a++; }  


      }
      ?>
    </table>

解决方法

由于您的问题被标记为<h2> Education </h2> <table> <tr> <td rowspan="3" width=20% style="border-top: 1px solid #cccccc; border-bottom: 1px solid #cccccc">ABMS Continuing Certification Directory</td> <td width=25% style="border-top: 1px solid #cccccc"><strong>Provider: </strong>MULTIPLE CME PROVIDERS OF BOARD-APPROVED MOC ACTIVITIES</td> <td rowspan="3" width=45% style="border-top: 1px solid #cccccc; border-bottom: 1px solid #cccccc"><strong>Overview: </strong>With its integrated search tool,the ABMS Continuing Certification Directory&#8482; helps diplomates identify relevant,quality continuing medical education (CME) activities that have been approved for MOC by participating ABMS Member Boards. MOC activities indexed in the Directory reflect the latest best practices,evidence-based guidelines and educational initiatives designed to support the development of high-functioning physicians. The Directory also offers physicians and CME providers access to additional continuing certification information and resources.</td> <td width=10% rowspan=3 style="border-top: 1px solid #cccccc; border-bottom: 1px solid #cccccc"><a href="https://www.continuingcertification.org/specialty-boards/">View</a></td> </tr> <tr> <td width=25% rowspan="2" style="border-bottom: 1px solid #cccccc"><strong>Content Area: </strong>ALL</td> </tr> </table> <table> <tr> <td rowspan="2" width=20% style="border-bottom: 1px solid #777777">Assessment Articles</td> <td width=25%><strong>Provider: </strong>MULTIPLE PROVIDERS</td> <td rowspan="2" width=45% style="border-bottom: 1px solid #777777"><strong>Overview: </strong>Articles available to choose from to include on your assessment.</td> <td width=10% rowspan=2 style="border-bottom: 1px solid #777777"><a href="#">View</a></td> </tr> <tr> <td width=25% style="border-bottom: 1px solid #777777"><strong>Content Area: </strong>ALL</td> </tr> </table> <h2> FAQs </h2> <table> <tr> <td rowspan="2" width=20% style="border-top: 1px solid #cccccc; border-bottom: 1px solid #777777">MOC FAQs</td> <td style="border-top: 1px solid #cccccc"><strong>Provider: </strong>ABMS</td> <td rowspan="2" width=45% style="border-top: 1px solid #cccccc; border-bottom: 1px solid #777777"><strong>Overview: </strong>Information about the CertLink Platform and Pilot Programs</td> <td width=10% rowspan=2 style="border-top: 1px solid #cccccc; border-bottom: 1px solid #777777"><a href="#">View</a></td> </tr> <tr> <td style="border-bottom: 1px solid #777777"><strong>Content Area: </strong>ALL</td> </tr> </table>,因此我假设您可以使用它。

首先,将功能更改为此:

jQuery

现在,为您的输入创建一个jQuery处理程序

function chachvalue(element) { 
    
    // get value of input that unfocused
    var x1 = parseInt(element.val()); 
    
    // select #sale_nag element
    var sale_nag = $('#sale_nag');

    // set value of sale_nag to sale_nag + x1
    sale_nag.val(parseInt(sale_nag.val()) + x1);

}

现在您的输入元素可以简化为

$('.outnag_input').on('focusout',function() {
    chachvalue($(this));
});

还应该将<input name="outnag[]" class="outnag_input" value="0" type="text"/> 函数和chachvalue()处理程序移到PHP循环的外部