jQuery - Chrome 和 Chrome Firefox OK - 资源管理器搞砸了?

发布于 2024-12-02 16:52:26 字数 2157 浏览 0 评论 0原文

我正在使用 Django 构建一个生成发票的 Web 应用程序。每张发票都有一个行项目列表,每个行项目都有一个总计。然后是小计、税收和总计行。小计、税金和总计值都是使用 jQuery 即时计算的。这在 Chrome 和 Firefox 中运行良好,但在 Explorer 中失败。在资源管理器中,仅选取第一个行项目。

这是我的小 jQuery 脚本:

<script type="text/javascript">
  $(document).ready(function() {
    var tot = 0;
    $('td#itemPrice').each(function() {
      tot = tot + parseFloat($(this).html());
      });
    $("td#sub_total_results").html(tot.toFixed(2));
    var gst = document.getElementById("gst").innerHTML;
    var tax = tot * parseFloat(gst);
    $("td#tax_results").html(tax.toFixed(2));
    var grand = tot + tax;
    $("td#grand_total_results").html("$" + grand.toFixed(2));
  });
</script>

这是包含行项目和总计的 HTML 块:

 <table class="lineItems_Container">
  <tr><td class="item_header" width=500 align=left>Item</td><td class="item_header" width=100 align=right>Price</td></tr>

  <tr>
    <td class="item_row">Labour</td>
    <td class="item_row" id="itemPrice" align=right>630.00</td>
  </tr>

  <tr>
    <td class="item_row">Product</td>
    <td class="item_row" id="itemPrice" align=right>75.00</td>
  </tr>

</table>
<br>
<div id="totals" style="float: right; width=200px;">
  <table class="totals_container">
    <tr>
<td class="totals_td" width=75 id="sub_total" style="font-size: 13px;">Sub Total:</td>
  <td class="totals_td" width=100 id="sub_total_results" align=right></td>
    </tr>
    <tr>
  <td class="totals_td" id="tax" style="font-size: 13px;">Tax:</td>
  <td class="totals_td"id="tax_results" align=right></td>
    </tr>

    <tr>
  <td class="totals_td" id="grand_total" style="font-size: 16px;">TOTAL:</td>
  <td class="totals_td" id="grand_total_results" align=right style="font-size: 16px;"></td>
    </tr>
  </table>
</div>

630.00 和 75.00 的值从调用此模板的 views.py 函数引入到模板中。

知道发生了什么以及为什么 IE 只使用第一个行项目吗?

谢谢

I am building a webapp with Django which generates invoices. Each invoice has a list of line items with each line item having a total. Then there is a Subtotal, a Tax, and a Grand total line. The Subtotal, Tax, and Grand Total values are all calculated on the fly using jQuery. This works nicely in Chrome and Firefox, but fails in Explorer. In Explorer, only the first line item is picked up.

Here's my little jQuery script:

<script type="text/javascript">
  $(document).ready(function() {
    var tot = 0;
    $('td#itemPrice').each(function() {
      tot = tot + parseFloat($(this).html());
      });
    $("td#sub_total_results").html(tot.toFixed(2));
    var gst = document.getElementById("gst").innerHTML;
    var tax = tot * parseFloat(gst);
    $("td#tax_results").html(tax.toFixed(2));
    var grand = tot + tax;
    $("td#grand_total_results").html("$" + grand.toFixed(2));
  });
</script>

And here is the chunk of HTML which has the line items and totals:

 <table class="lineItems_Container">
  <tr><td class="item_header" width=500 align=left>Item</td><td class="item_header" width=100 align=right>Price</td></tr>

  <tr>
    <td class="item_row">Labour</td>
    <td class="item_row" id="itemPrice" align=right>630.00</td>
  </tr>

  <tr>
    <td class="item_row">Product</td>
    <td class="item_row" id="itemPrice" align=right>75.00</td>
  </tr>

</table>
<br>
<div id="totals" style="float: right; width=200px;">
  <table class="totals_container">
    <tr>
<td class="totals_td" width=75 id="sub_total" style="font-size: 13px;">Sub Total:</td>
  <td class="totals_td" width=100 id="sub_total_results" align=right></td>
    </tr>
    <tr>
  <td class="totals_td" id="tax" style="font-size: 13px;">Tax:</td>
  <td class="totals_td"id="tax_results" align=right></td>
    </tr>

    <tr>
  <td class="totals_td" id="grand_total" style="font-size: 16px;">TOTAL:</td>
  <td class="totals_td" id="grand_total_results" align=right style="font-size: 16px;"></td>
    </tr>
  </table>
</div>

The values of 630.00 and 75.00 are brought into the template from the views.py function that calls this template.

Any ideas what is going on and why IE is only using the first line item?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

悲念泪 2024-12-09 16:52:26

您的 HTML 无效:页面上不应有多个具有相同 ID 的元素。如果这样做,并且尝试根据 ID 进行选择,您将在浏览器之间得到不一致的结果(如您所见)。

您需要使用非 ID 方法来选择 itemPrice 单元格。一种方法是使 itemPrice 成为一个类而不是 ID - 在这种情况下,该类不会用于应用任何样式,它只是为了从代码中轻松选择(如果您不这样做)还没有意识到这一点,通过说 class="class1 class2 class3 etc" 将多个类分配给同一个元素是完全合法的:

<tr>
  <td class="item_row">Labour</td>
  <td class="item_row itemPrice" align=right>630.00</td>
</tr>
<tr>
  <td class="item_row">Product</td>
  <td class="item_row itemPrice" align=right>75.00</td>
</tr> 

<script>
// select by class instead of ID
$('td.itemPrice').each(function() {
  ...
});
</script>

另一种方法可能是使用一个选择器取每个 TR 的最后一个孩子。

Your HTML is invalid: you're not supposed to have more than one element on the page with the same ID. If you do, and you try to select based on ID, you'll get inconsistent results between browsers (as you have seen).

You need a non-ID method of selected the itemPrice cells. One way is to make itemPrice a class rather than an ID - in this case the class wouldn't be used to apply any styles, it is solely for easy selection from your code (in case you weren't already of aware of it, it is perfectly legal to assign multiple classes to the same element by saying class="class1 class2 class3 etc"):

<tr>
  <td class="item_row">Labour</td>
  <td class="item_row itemPrice" align=right>630.00</td>
</tr>
<tr>
  <td class="item_row">Product</td>
  <td class="item_row itemPrice" align=right>75.00</td>
</tr> 

<script>
// select by class instead of ID
$('td.itemPrice').each(function() {
  ...
});
</script>

Another way to do it might be to use a selector that takes the last child of each TR.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文