jQuery循环计算

时间:2016-02-18 11:20:07

标签: javascript jquery

我正在尝试使用php带来一些记录并进行一些计算。我现在正在做的是,每一行都有不同货币的下拉列表。当我选择每种货币时,它会计算并显示某些值。直到这里工作正常。

我想要实现的是,如果我选择第一个货币下拉列表,它应该计算完整的记录计算,而不是选择每行的货币。我想我需要在计算行的jquery中做一些循环。

Fiddle

小提琴你可以看到代码,但没有正确获得输出。所以我只是将代码放在服务器中。链接是http://spreadon.net/quot.html

以下是货币下拉列表的jquery脚本的一部分。

$(window).load(function() {
  $(document).ready(function() {
    $("select").on('change', function() {

      var dept_number = $(this).val();
      var price = $(this).find(':selected').data('price');
      var selected = $(this).find('option:selected').text();

      if (selected == "INR") {
        $(this).closest('table').find('.total1').val($(this).closest('table').find('.total').val());
      } else {
        $(this).closest('table').find('.total1').val((($(this).closest('table').find('.total').val() * price) / $(this).closest('table').find('.inrvalue').val()).toFixed(3));
      }

      $(this).closest('table').find('.price_unit').val(($(this).closest('table').find('.total1').val() / $(this).closest('table').find('.qty').val()).toFixed(3));

    });
  });
});

1 个答案:

答案 0 :(得分:0)

我添加了工作代码,它将指导您如何迭代tr和它更改选择

查看它会给你提示的代码。

并且你在每种方法中都有这种力量,因此你可以实现自己的目标。



 $(document).ready(function() {
   $("select").on('change', function() {

     var dept_number = $(this).val();
     var price = $(this).find(':selected').data('price');

     var selected = $(this).find('option:selected').text();

     if (selected == "INR") {
       $(this).closest('table').find('.total1').val($(this).closest('table').find('.total').val());

     } else {

       $(this).closest('table').find('.total1').val((($(this).closest('table').find('.total').val() * price) / $(this).closest('table').find('.inrvalue').val()).toFixed(3));
     }

     $(this).closest('table').find('.price_unit').val(($(this).closest('table').find('.total1').val() / $(this).closest('table').find('.qty').val()).toFixed(3));


     // this is upto your logic.

     //now i have write my code which will ittrate through each row and column

     $(".trToiitrate").each(function(e) {
       alert("it is from tr");
       $(this).find("td").each(function(e) {
         alert("this is from td");
       });
     });

   });




 });

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form name='cart' method='post' class='single' action='generate_quot_cust_edititems_save_complete.php?tender_id=151399'>
  <input size='1' type='hidden' name='id[3]' value='1582' readonly/>
  <input type='hidden' name='sum_input' id='sum_input' value=''>
  <div align="center">
    <table width="100%" border="1" style="border-collapse: collapse;">
      <thead>
        <tr bgcolor="#E6E6FA">
          <th width=6%>SlNo</th>
          <th width=15%>Description</th>
          <th width=5%>Qty</th>
          <th width=5%>Unit Price</th>
          <th width=5%>Total</th>
          <th width=8%>Total (INR)</th>
          <th width=7%>Total Value</th>
          <th width=7%>Currency</th>
          <th width=7%>Total</th>
          <th width=7%>Price/Unit</th>
        </tr>
      </thead>
    </table>
  </div>
  <div align="center" class="base">
    <table width="100%" border="1" style="border-collapse: collapse;">
      <tbody id="people">
        <tr class="trToiitrate">
          <td width='6%'>
            <input size='1' type='hidden' name='id[0]' value='1579' readonly/>
            <input size='1' type='text' name='sl[0]' value='1' readonly/>
          </td>
          <td width='15%' id='addinput'>
            <input type='text' size='19' id='item_name0' name='item_name[0]' placeholder='3M EC 2214 Regular Adhesive EpoxySize: 150ml box ' value='3M EC 2214 Regular Adhesive EpoxySize: 150ml box ' />
          </td>
          <td width='5%'>
            <input size='1' class='qty' type='text' name='qty[]' id='qty[]' value='10' readonly/>
          </td>
          <td width='5%'>
            <input size='1' type='text' name='unitprice[0]' value='223.15' readonly/>
          </td>
          <td width='5%'>
            <input size='5' type='text' name='total_old' value='2231.500' readonly/>
          </td>
          <td width='8%'>
            <input size='7' type='text' id='total_inr[]' name='total_inr[]' value='219479.000' />
          </td>
          <input class='txt' type='hidden' size='3' id='addon_value[]' name='addon_value[]' value='0' onchange='calcTotals()'>
          <td width='7%'>
            <input class='total' size='6' type='text' id='add_value[]' name='add_value[]' value='373114'>
          </td>
          <td width='7%'>
            <select id='currency_change[]' name='currency_change[]'>
              <option value=''>select</option>
              <option value=USD data-price=1>USD</option>
              <option value=INR data-price=68.128019>INR</option>
              <option value=GBP data-price=0.692675>GBP</option>
              <option value=EUR data-price=0.895982>EUR</option>
              <option selected="selected" value=SGD data-price=1.400105>SGD</option>
              <option value=AUD data-price=1.3949>AUD</option>
              <option value=CAD data-price=1.377725>CAD</option>
              <option value=CHF data-price=0.986467>CHF</option>
              <option value=JPY data-price=114.6032>JPY</option>
              <option value=MYR data-price=4.136531>MYR</option>
              <option value=ZAR data-price=15.73123>ZAR</option>
            </select>
          </td>
          <input type="hidden" class="inrvalue" id="inrvalue" name="inrvalue" value="68.128019">
          <input type="hidden" class="deptip" id="dept-input" />
          <input type="hidden" class="priceip" id="price-input" />
          <input type="hidden" class="cs" id="cs" name="cs" value="SGD">
          <td width="7%">
            <input size="5" type="text" data-value="" name='total1[]' id='total1[]' value="13531.598" readonly class="total1" />
          </td>
          <td width='7%'>
            <input class='price_unit' size='6' type='text' id='price_unit[]' name='price_unit[]' value='1353.160' readonly>
          </td>
        </tr>

        <div align="center" class="base">
          <table width="100%" border="1" style="border-collapse: collapse;">
            <tbody id="people">
              <tr class="trToiitrate">
                <td width='6%'>
                  <input size='1' type='hidden' name='id[1]' value='1580' readonly/>
                  <input size='1' type='text' name='sl[1]' value='2' readonly/>
                </td>
                <td width='15%' id='addinput'>
                  <input type='text' size='19' id='item_name1' name='item_name[1]' placeholder='CONAP CE-1170 ACRYLIC CONFORMAL COATING AS PER MIL-1-46058, Type AR,Size: 01 quart = 0.8 kg' value='CONAP CE-1170 ACRYLIC CONFORMAL COATING AS PER MIL-1-46058, Type AR,Size: 01 quart = 0.8 kg'
                  />
                </td>
                <td width='5%'>
                  <input size='1' class='qty' type='text' name='qty[]' id='qty[]' value='2' readonly/>
                </td>
                <td width='5%'>
                  <input size='1' type='text' name='unitprice[1]' value='285' readonly/>
                </td>
                <td width='5%'>
                  <input size='5' type='text' name='total_old' value='570.000' readonly/>
                </td>
                <td width='8%'>
                  <input size='7' type='text' id='total_inr[]' name='total_inr[]' value='27735.800' />
                </td>
                <input class='txt' type='hidden' size='3' id='addon_value[]' name='addon_value[]' value='0' onchange='calcTotals()'>
                <td width='7%'>
                  <input class='total' size='6' type='text' id='add_value[]' name='add_value[]' value='83207.4'>
                </td>
                <td width='7%'>
                  <select id='currency_change[]' name='currency_change[]'>
                    <option value=''>select</option>
                    <option value=USD data-price=1>USD</option>
                    <option value=INR data-price=68.128019>INR</option>
                    <option value=GBP data-price=0.692675>GBP</option>
                    <option value=EUR data-price=0.895982>EUR</option>
                    <option selected="selected" value=SGD data-price=1.400105>SGD</option>
                    <option value=AUD data-price=1.3949>AUD</option>
                    <option value=CAD data-price=1.377725>CAD</option>
                    <option value=CHF data-price=0.986467>CHF</option>
                    <option value=JPY data-price=114.6032>JPY</option>
                    <option value=MYR data-price=4.136531>MYR</option>
                    <option value=ZAR data-price=15.73123>ZAR</option>
                  </select>
                </td>
                <input type="hidden" class="inrvalue" id="inrvalue" name="inrvalue" value="68.128019">
                <input type="hidden" class="deptip" id="dept-input" />
                <input type="hidden" class="priceip" id="price-input" />
                <input type="hidden" class="cs" id="cs" name="cs" value="SGD">
                <td width="7%">
                  <input size="5" type="text" data-value="" name='total1[]' id='total1[]' value="1710.003" readonly class="total1" />
                </td>
                <td width='7%'>
                  <input class='price_unit' size='6' type='text' id='price_unit[]' name='price_unit[]' value='855.001' readonly>
                </td>
              </tr>
              <div align="center" class="base">
&#13;
&#13;
&#13;