简体   繁体   English

复选框中的检查值返回 NaN

[英]Checked values from checkboxes return NaN

I have a website about medicines and i want to calculate the total sum of checked medicines (i have price) and for that im using a JS script but when i check the checkboxes the return i get is "NaN" instead of the price.我有一个关于药品的网站,我想计算检查药品的总和(我有价格),为此我使用 JS 脚本,但是当我选中复选框时,我得到的回报是“NaN”而不是价格。 Can i get some help solving this issue?我可以得到一些帮助来解决这个问题吗?

Here is my Table:这是我的表:

 @foreach (var item in Model)
        {
            <tr>
                <td>
                    @Html.DisplayFor(modelItem => item.Name)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.Quantity)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.Price)
                </td>

                <td>
                    @if (User.IsInRole("Administrator"))
                    {
                        @Html.ActionLink("Edit", "Edit", new { id = item.Id }, new { @class = "btn btn-info" })
                        <button data-id="@item.Id" class="btn btn-warning js-delete">Delete</button>
                    }
                    @if (User.IsInRole("Pharmacist"))
                    {

                        @item.Id<input type="checkbox" name="selectedNames" value="@item.Id" />

                    }
                </td>
            </tr>
        }
    <p id="tot"></p>

and here is my JS:这是我的JS:

$(function () {
        var total;
        var checked = $('input:checkbox').click(function (e) {
            calculateSum();
        });
         function calculateSum() {
            var $checked = $(':checkbox:checked');
            total = 0.0;
            $checked.each(function () {
                total += parseFloat($(this).next().text());
            });
            $('#tot').text("Total Amount: " + total.toFixed(2));
        }
    })

Consider the following example.考虑以下示例。

 $(function() { var total; function calculateSum() { total = 0.0; $("input[type='checkbox']:checked").each(function(i, el) { total += parseFloat($(el).closest("tr").find("td").eq(2).text()); }); $('#tot').html("Total Amount: " + total.toFixed(2)); } $('input:checkbox').click(calculateSum); })
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <table> <thead> <tr> <th>Name</th> <th>Qnty</th> <th>Price</th> <td>&nbsp;</td> </tr> </thead> <tbody> <tr> <td>Item 1</td> <td>10</td> <td>5.00</td> <td> <button data-id="item-1" class="btn btn-warning js-delete">Delete</button> <input type="checkbox" name="selectedNames" value="item-1" /> </td> </tr> <tr> <td>Item 2</td> <td>20</td> <td>10.00</td> <td> <button data-id="item-2" class="btn btn-warning js-delete">Delete</button> <input type="checkbox" name="selectedNames" value="item-2" /> </td> </tr> <tr> <td>Item 3</td> <td>30</td> <td>15.00</td> <td> <button data-id="item-3" class="btn btn-warning js-delete">Delete</button> <input type="checkbox" name="selectedNames" value="item-3" /> </td> </tr> </table> <p id="tot"></p>

This uses a similar structure of HTML.这使用了类似的结构 HTML。 When the User clicks a checkbox, the click callback is called.当用户单击复选框时,将调用click回调。 A named function can be used as the callback.一个名为 function 可以用作回调。

The calculation function runs and checks for checked checkboxes.计算 function 运行并检查checked的复选框。 When found, it seeks the price column (from a 0 index, this is column 2).找到后,它会查找price列(从 0 索引,这是第 2 列)。 Once parsed from Text, it is added to the total .一旦从 Text 中解析出来,它就会被添加到total中。

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

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