在表行中查找班级

发布于 2024-08-24 01:40:58 字数 1153 浏览 3 评论 0原文

看一下这个表:

<table cellpadding="0" cellspacing="0" class="order_form">
    <tr>
        <th>Amount</th>
        <th>Desc</th>
        <th>Price</th>
        <th>Total</th>
    </tr>
    <tr>
        <td><input type="text" class="order_count" /></td>
        <td>
            <span class="order_desc">Middagstallerken</span>
        </td>
        <td>
            <span class="order_price">1,15</span>
        </td>
        <td>
            <span class="order_each_total"></span>
        </td>
    </tr>
    [...]
</table>

输入金额后,我需要选择“order_price”类,并将其与输入“order_count”的值相乘,然后将其放入“order_each_count”中。我有很多这样的行,所以我需要找到该行中的下一个类。

我尝试过使用这样的函数,但没有结果:

<script type="text/javascript">
    $(document).ready(function(){
        $('.order_count').keyup(function() {
            var each_price = $(this).prevUntil("tr").find("span.order_price").text();
        });
     });
</script>

我希望有人有一个好的解决方案:-)

Take a look at this table:

<table cellpadding="0" cellspacing="0" class="order_form">
    <tr>
        <th>Amount</th>
        <th>Desc</th>
        <th>Price</th>
        <th>Total</th>
    </tr>
    <tr>
        <td><input type="text" class="order_count" /></td>
        <td>
            <span class="order_desc">Middagstallerken</span>
        </td>
        <td>
            <span class="order_price">1,15</span>
        </td>
        <td>
            <span class="order_each_total"></span>
        </td>
    </tr>
    [...]
</table>

Upon entering amount I need to select the class "order_price" and multiply it with the value of the input "order_count" and place it in "order_each_count". I have a lot of these rows so I need to find the next class in the row.

I have tried using some function like this but without result:

<script type="text/javascript">
    $(document).ready(function(){
        $('.order_count').keyup(function() {
            var each_price = $(this).prevUntil("tr").find("span.order_price").text();
        });
     });
</script>

I hope someone have a good solution :-)

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

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

发布评论

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

评论(1

倾城月光淡如水﹏ 2024-08-31 01:40:58

使用 closest() 而不是 prevUntil

$(document).ready(function(){
    $('.order_count').keyup(function() {
        var amount = parseInt($(this).val(), 10);
        var each_price = $(this)
                             .closest('tr')
                             .find('span.order_price')
                             .text()
                             .replace(',', '.'); // Floats use . as separator

        each_price  = parseFloat(each_price);
        total_price = amount * each_price;

        // Update the value
        $(this)
            .closest('tr')
            .find('span.order_each_total')
            .text(total_price
                .toFixed(2) // "Round" to two decimal places
                .replace('.', ',') // Format properly
            );
    });
 });

记住使用 parseFloatparseInt当尝试在计算中使用 DOM 中的数字时 - 默认情况下它们是字符串。

Use closest() instead of prevUntil:

$(document).ready(function(){
    $('.order_count').keyup(function() {
        var amount = parseInt($(this).val(), 10);
        var each_price = $(this)
                             .closest('tr')
                             .find('span.order_price')
                             .text()
                             .replace(',', '.'); // Floats use . as separator

        each_price  = parseFloat(each_price);
        total_price = amount * each_price;

        // Update the value
        $(this)
            .closest('tr')
            .find('span.order_each_total')
            .text(total_price
                .toFixed(2) // "Round" to two decimal places
                .replace('.', ',') // Format properly
            );
    });
 });

Remember to use parseFloat or parseInt when trying to use numbers from DOM in calculations – they're strings as default.

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