javascript - 计算所选复选框的总价

标签 javascript php laravel

我有这一段代码:

<form action="{{ URL::action('server-shop-post') }}" method="post" id="cart">
    <table class="table table-responsive">
        <thead>
            <tr><th>Check</th><th>Item</th><th>Pixel Points <span class="fa fa-cubes"></span></th></tr>
        </thead>
        <tbody>
            @foreach($shopItems as $shopItem)
                <tr>
                    <td>
                        @if(!in_array($shopItem -> id, $userPurchases))
                            <input form="cart" name="cart[]" value="{{ $shopItem -> id }}" type="checkbox">
                        @else
                            <span class="glyphicon glyphicon-ok"></span>
                        @endif
                    </td>
                    <td>
                        {{ $shopItem -> name }}
                    </td>
                    <td>
                        {{ $shopItem -> cost }}
                    </td>
                </tr>
            @endforeach
            <tr><td colspan="2">You have <span class="fa fa-cubes"></span> {{ Auth::user() -> pixel_points }}</td><td>Total cost: 200</td></tr>
        </tbody>
    </table><input type="submit">
</form>

它的作用是创建一个充满商店商品的表格,并允许人们选中复选框以“添加到购物篮”。然后价格也会显示在与复选框相同的行中。 我的目标是让用户检查一个项目,并随之更新总成本。 如何将选中复选框的所有值添加到一起?

最佳答案

如果列表很大,则迭代所有列表项的成本可能会很高。因此,将“onchange”事件处理程序绑定(bind)到每个复选框。

$( ".aCommonClass" ).change(function() {
  //check if it was checked or unchecked.
  //based on that, add or sub the value from the total 
});

关于javascript - 计算所选复选框的总价,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26723139/

相关文章:

javascript - javascript中的动态值到对象中

javascript - Requirejs 出现媒体元素错误

javascript - 如何使用 Adi Palaz 的嵌套 Accordion 展开全部/折叠全部

javascript - Jquery dotdotdot 仅在窗口调整大小时正确触发

php - 返回插入行的 id(自动递增)

php - 我怎样才能制作一个水平滚动的div,里面有div?

php - 一英里长的 "include"文件是否会导致页面速度变慢?

javascript - Ajax 从 Laravel 接收对象集合

php - 如何使用 laravel 处理 mysql 中的大型更新查询

forms - 为什么 Laravel 5.4 中表单不提交