繁体   English   中英

使用 Jquery 从输入中获取值

[英]Get value from an input with Jquery

我找到了很多这类问题的答案,但没有一个能解决问题。 我什至有一段非常相似的代码可以工作。 但是,这不断给出NaN 我已将范围缩小到无法获得

var or = parseFloat($(this).siblings("input[name='or']").val());

这是我所拥有的。 任何帮助是极大的赞赏!

<tbody>
  {% for i in t %}
  <tr class="hotlines">
    <td><strong>{{i.owner}}</strong><br>{{i.owner_id}}</td>
    <td>
      <li>{{i.tot_itm}}</li>
      <li>Per Piece Profit: {{"${:,.2f}".format(i.item_prof)}}</li>
    </td>
    <td>
      <li>Payable Hotlines <br><input type="text" name="hln" maxlength="4" value=""></li>
      <li>Per Hotline Profit: {{"${:,.2f}".format(i.hotline_prof)}}</li>
      <input type="text" name="or" value="{{i.hotline_prof}}">
    </td>
    <td>
      <li>Total Backpack Profit: {{"${:,.2f}".format(i.tot_prof)}}</li>
      <li>Total Hotline Profit: <input type="text" name="hlnt" readonly /></li>
    </td>
  </tr>
  <!-- Total items sent from db -->
  <input class="hidden" type="text" name="it" value="{{i.tot_itm}}">
  <!-- per hotline override from db -->

  <!-- profit from all items * per item override sent from server -->
  <input class="hidden" type="text" name="bp" value="{{i.tot_prof}}">
  <!-- total proffit from items and hotlines -->
  <input class="hidden" type="text" name="th">
  <!-- per item override from db -->
  <input class="hidden" type="text" name="or_it" value="{{i.item_prof}}">
  <!-- total proffit from hotlines -->
  <input class="hidden" type="text" name="hl" value="0">
  <!-- organization ID -->
  <input class="hidden" type="text" name="o" value="{{org.organization_ID}}">
  <input class="hidden" type="text" name="owner_n" value="{{i.owner}}">
  <input class="hidden" type="text" name="owner_id" value="{{i.id}}">
  {% endfor %}
  <td><button type="submit" class="btn btn-link btn-xs btn-block">Submit Report</button></td>
</tbody>

$(document).on("keyup", "[name='hln']", function () {
  $this = $(this);
  //var or = 0
  var pro = 0
  //using native dom Element.closest() method
  //https://developer.mozilla.org/en-US/docs/Web/API/Element/closest
  let parent = $(this.closest('.hotlines'));

  //or using jQuery's closest();
  //let parent = $this.closest('.hotlines');

  var value = parseFloat($this.val());

  var or = parseFloat($(this).siblings("input[name='or']").val());
  var bp = parseFloat($(this).siblings("input[name='bp']").val());
  //var other = parseFloat($(this).siblings("input[name='amt']").val());
  pro = (value * or);
  var total = (pro + bp);

  //use find() to find the child element
  parent.find('input[name="hlnt"]').val("$" + pro.toFixed(2));
  parent.find('input[name="total"]').val("$" + total.toFixed(2));
  parent.find('input[name="th"]').val(total);
  parent.find('input[name="hl"]').val(pro);
});

在 Tyler Roper 的指导下,这是一个与 Javascript 无关的简单修复。 我很累,正在测试东西,并试图快速将页面转换为表格。 糟糕的 HTML 是原因。 为了解决它,我只是改为:

<form action="/weekly_reports/" method="POST">
                  <table  id="office_weekly" class="table table-hover table-striped">
                      <thead>
                        <th>Office</th>
                        <th>Backpacks</th>
                        <th>Hotlines</th>
                        <th>Total</th>
                      </thead>
                      <tbody>
                        {% for i in t %}
                          <tr class="hotlines">
                            <td><strong>{{i.owner}}</strong><br>{{i.owner_id}}</td>
                            <td>
                              {{i.tot_itm}}<br>
                              Per Piece Profit: {{"${:,.2f}".format(i.item_prof)}}
                            </td>
                            <td>
                              Payable Hotlines <br><input type="text" name="hln" maxlength="4" value=""><br>
                              Per Hotline Profit: {{"${:,.2f}".format(i.hotline_prof)}}<br>
                              <input type="hidden" name="or" value="{{i.hotline_prof}}">
                            </td>
                            <td>
                              Total Backpack Profit: {{"${:,.2f}".format(i.tot_prof)}}
                            </td>
                            <td>Total Hotline Profit: <input type="text" name="hlnt" readonly /></td>
                          </tr>
                        {% endfor %}
                        <td><button type="submit" class="btn btn-link btn-xs btn-block">Submit Report</button></td>
                      </tbody>
                    </table>
                </form>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM