如何显示 span 标记之间复选框的计算



我现在有了这段javascript,用于计算,效果很好。

$(document).ready( function() {
$(".checkboxes").click(
    function () {
        var ntot = 0;
        $(".checkboxes:checked").each(function () {
            ntot += parseInt($(this).val());
        });
        $(".totaalprijs").val(ntot);
    })
    .change();
});

形式:

<form id="form1" name="form1" method="post" action="">
                Check this for 10% savings
                <input class="checkboxes" type="checkbox" value="10" />
                <br />
                Check this for 15% savings
                <input class="checkboxes" type="checkbox" value="15" />
                <br />
                Check this for 20% savings
                <input class="checkboxes" type="checkbox" value="20" />
                <br />
            </form>
                <input type="text" class="totalprice"  size="5" />

输出显示在最后一个输入中,class="totalprice">

我怎样才能在 class="totalprice" 的 span 标签中做到这一点?

所以这个:

     <input type="text" class="totalprice"  size="5" />

应替换为以下内容:

<span class="totalprice"><!-- output value should be here--></span>

使用 .text() 而不是 .val()

$(".totalprice").text(ntot);

你只是有一个错别字

你的 js 是

$(".totaalprijs").val(ntot);

您的输入是

<input type="text" class="totalprice"  size="5" />

totaalprijs不是totalprice.

所以只要纠正错别字,你就好:)

此外,如果要在 SPAN 中显示,可以使用 .text().html()

最新更新