gpt4 book ai didi

javascript - Keyup:选中复选框时添加值

转载 作者:行者123 更新时间:2023-12-03 11:19:08 28 4
gpt4 key购买 nike

所以我使用 Keyup 来计算输入字段的总数。到目前为止,一切都很好。但现在我想这样做:如果选中#a 复选框,则将总金额加 12。如果没有,则添加 0。

我的代码基于此:http://jsfiddle.net/5xzSy/1/

$(document).keyup(function(){ // run anytime the value changes
var element1 = parseFloat($('#element1').val()) * 16.28 || 0; // get value of field and multiply by price
var total = 0+element1
});

如何添加已选中复选框的值并在人们取消选中它时减去它。(或者更好:使用单选按钮?)谢谢!

最佳答案

演示:http://jsfiddle.net/5xzSy/1079/ <-- 已更新(仅在给出某些内容时添加 12, float )

顺便说一句:input<input/>而不是<input></input>

$('input').keyup(function(){ // run anytime the value changes        
var firstValue = parseFloat($('#first').val()); // get value of field
var secondValue = parseFloat($('#second').val()); // convert it to a float
var thirdValue = parseFloat($('#third').val());
$('#added').html(firstValue + secondValue + thirdValue); // add them and output it
});

$('#check').on('change', function () {
var total = parseFloat($('#added').text()); // <-- update for float
if (total) {
if ($(this).is(':checked')) {
total = total + 12;
} else {
total = total - 12;
}
$('#added').text(total);
}
})

关于javascript - Keyup:选中复选框时添加值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27200871/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com