在Jquery中总计选定的复选框值

时间:2015-11-19 07:24:48

标签: jquery

我正在检查特定复选框的总计。代码工作正常。如果没有选中任何复选框,我如何查看所有项目值的格式。

   $(function () {
   $("input[type='checkbox'").on("click", function () {
       recalcTotal();
    });

   function recalcTotal() {
        var total = 0;

        $("input:checked").each(function () {
            total += $(this).next("input").val() * 1;
        });

        $("#total").val(total);
    }
});

<input type="checkbox" name="values"/><input type="text" readonly value="100"/> <br />
<input type="checkbox" name="values" /><input type="text" readonly value="200"/> <br />
<input type="checkbox" name="values" /><input type="text" readonly value="300"/> <br />
<input type="checkbox" name="values" /><input type="text" readonly value="400"/> <br />
<input type="checkbox" name="values" /><input type="text" readonly value="500"/> <br />
<input type="checkbox" name="values" /><input type="text" readonly value="600"/> <br /><br />
<b>Grand Total:</b> <input type="text" id="total" readonly/> <br />

1 个答案:

答案 0 :(得分:1)

你可以简单地修改处理程序,根据已检查元素的数量迭代检查和非检查。另请注意:

1)您还应该在每个循环中使用2015-11-30 23:59:59解析值:

2)使用更改事件而不是单击。

3)使用#if WINDOWS_PHONE || __ANDROID__ Debug.WriteLine("Passed"); #endif 在附加事件后触发更改。这将在开头填充总金额。

{
    "outputs": [
        {
            "ports": {
                "1521/tcp/oracle_tnslsnr": [
                    {
                        "hostname": "172.27.64.253"
                    }
                ]
            },
            "has_attachment": 0,
            "custom_description": null,
            "plugin_output": "Nothing here",
            "hosts": null,
            "severity": 3
        }
    ]
}

<强> Working Demo