获取JavaScript表单的总量

获取JavaScript表单的总量,javascript,jquery,Javascript,Jquery,我仍在学习JavaScript,需要帮助更新我的脚本以记录表单中的多个项目。我在表单上使用此脚本获取单选按钮的值,并将该值添加到事件签出表单的“金额”字段中 $(function(){ $('.ticketAmount').click(function() { $('#Amount').val(this.value); }); }); 现在,我有了一个新的表单,它有一些选项与票务选择一起使用。现在,客户选择车票并有几个复选框来添加私人旅行和各种商品 以下是我的H

我仍在学习JavaScript,需要帮助更新我的脚本以记录表单中的多个项目。我在表单上使用此脚本获取单选按钮的值,并将该值添加到事件签出表单的“金额”字段中

$(function(){
    $('.ticketAmount').click(function() {
        $('#Amount').val(this.value);
    });
});
现在,我有了一个新的表单,它有一些选项与票务选择一起使用。现在,客户选择车票并有几个复选框来添加私人旅行和各种商品

以下是我的HTML代码:

<div>
    <input type="radio" name="da" value="150" class="ticketAmount" />
    <strong>Regular Ticket $150.00</strong>
</div>
<div>
    <input type="radio" name="da" value="250" class="ticketAmount" />
    <strong>Couples Ticket $250.00</strong>
</div>
  <div>
    <input type="radio" name="da" value="1500" class="ticketAmount" />
    <strong>Table (Seats 10 Individuals) 1500.00</strong>
</div>
<div>
    <input type="checkbox" name="da" value="50" class="ticketAddition" >
    <strong>Private Tour $50.00</strong>
</div>
<div>
    <input type="checkbox" name="da" value="25" class="ticketAddition" >
    <strong>Meet the Chef $25.00</strong>
</div>

普通票$150.00
情侣票$250.00
餐桌(10人座)1500.00
私人旅行50.00美元
与厨师见面$25.00

我希望底部的复选框在选中时将其值添加到总金额中。任何帮助都将不胜感激。

当按钮被选中时,将值加到总数中,然后当按钮被取消选中时,将值减去。这可以通过
onchange
事件或
.change()
jQuery方法完成

$(函数(){
//单选按钮的ticketmount,复选框的ticketAddition
var ticketAmount=0,ticketadition=0;
$(“.ticketAmount”).change(函数(){
//将ticketmount设置为选中按钮的值。
var value=parseInt(this.value);
ticketAmount=值;
$(“#金额”).text((ticketAmount+ticketadition)+.00);
});
$(“.ticketadition”).change(函数(){
//从ticketAddition中添加或减去按钮的值
//取决于按钮是否已选中或未选中。
var value=parseInt(this.value);
如果(选中此项)ticketAddition+=值;
else ticketadition-=值;
$(“#金额”).text((ticketAmount+ticketadition)+.00);
});
});

普通票$150.00
情侣票$250.00
餐桌(10人座)1500.00
私人旅行50.00美元
与厨师见面$25.00
总计:$0.00
试试这个

<div>
    <input type="radio" name="da" value="150" class="ticketAmount" />
    <strong>Regular Ticket $150.00</strong>
</div>
<div>
    <input type="radio" name="da" value="250" class="ticketAmount" />
    <strong>Couples Ticket $250.00</strong>
</div>
  <div>
    <input type="radio" name="da" value="1500" class="ticketAmount" />
    <strong>Table (Seats 10 Individuals) 1500.00</strong>
</div>
<div>
    <input type="checkbox" name="da1" value="50" class="ticketAddition" >
    <strong>Private Tour $50.00</strong>
</div>
<div>
    <input type="checkbox" name="da1" value="25" class="ticketAddition" >
    <strong>Meet the Chef $25.00</strong>
</div>


$('.ticketAmount,.ticketAddition').click(function () {
    var total = parseInt($('input[name=da]:checked').val());
    $('input[name=da1]:checked').each(function () {
        total += parseInt($(this).val());
    });
    alert(total);
});

普通票$150.00
情侣票$250.00
餐桌(10人座)1500.00
私人旅行50.00美元
与厨师见面$25.00
$('.ticketAmount,.ticketAddition')。单击(函数(){
var total=parseInt($('input[name=da]:checked').val();
$('input[name=da1]:选中')。每个(函数(){
total+=parseInt($(this.val());
});
警报(总数);
});

以下是我的方法,我认为这是最简单的解决方案

由于这些是货币,我使用float并将金额削减到两位小数

Html是

<div>
    <input type="radio" name="da" value="150" class="ticketAmount" />
    <strong>Regular Ticket $150.00</strong>
</div>
<div>
    <input type="radio" name="da" value="250" class="ticketAmount" />
    <strong>Couples Ticket $250.00</strong>
</div>
  <div>
    <input type="radio" name="da" value="1500" class="ticketAmount" />
    <strong>Table (Seats 10 Individuals) 1500.00</strong>
</div>
<div>
    <input type="checkbox" id="privateTour" value="50" class="ticketAddition" >
    <strong>Private Tour $50.00</strong>
</div>
<div>
    <input type="checkbox" id="meetTheChef" value="25" class="ticketAddition" >
    <strong>Meet the Chef $25.00</strong>
</div>
    <input type="text" id="Amount">

普通票$150.00
情侣票$250.00
餐桌(10人座)1500.00
私人旅行50.00美元
与厨师见面$25.00

金额是什么类型的要素@urbz我想你的意思是:在JS:PAre中小心使用字符串/int的
+
,你在找这样的东西吗?
$(function(){
    $('.ticketAmount,#privateTour,#meetTheChef').click(function() {
        CalculateAmount();
    });

    function CalculateAmount()
    {
        var ticketPrice = parseFloat($('.ticketAmount:checked').val());
        var otherPrices = 0.0;
        if($("#meetTheChef").is(':checked'))
            otherPrices = parseFloat($("#meetTheChef").val());
        if($("#privateTour").is(':checked'))
            otherPrices = otherPrices + parseFloat($("#privateTour").val());
        $('#Amount').val((ticketPrice + otherPrices).toFixed(2));
    }
});
<div>
    <input type="radio" name="da" value="150" class="ticketAmount" />
    <strong>Regular Ticket $150.00</strong>
</div>
<div>
    <input type="radio" name="da" value="250" class="ticketAmount" />
    <strong>Couples Ticket $250.00</strong>
</div>
  <div>
    <input type="radio" name="da" value="1500" class="ticketAmount" />
    <strong>Table (Seats 10 Individuals) 1500.00</strong>
</div>
<div>
    <input type="checkbox" id="privateTour" value="50" class="ticketAddition" >
    <strong>Private Tour $50.00</strong>
</div>
<div>
    <input type="checkbox" id="meetTheChef" value="25" class="ticketAddition" >
    <strong>Meet the Chef $25.00</strong>
</div>
    <input type="text" id="Amount">