我在这里很新,所以这个问题对您来说似乎很愚蠢。
我有一个网站,其中显示了一些复选框。这些复选框具有一些需要与价格相加的值,并且还具有需要在div中显示的文本。
Here's the demo website,如您所见,复选框需要将自己的价格与橙色pices(它们具有两个不同的divs
)相加,然后将值插入div
内与id=plus-display
。
就目前而言,我猜到了这种解决方案,但它根本无法正常工作。
var basicPrice = 784 ; // This is how we start
function getCheck() {
var currentPrice = basicPrice; // every time
CurrentPrice = basicPrice,
plus = [],
total = 0;
console.log(currentPrice)
$("input[id^=plus]").each(function() {
if (this.checked) {
total += +this.value;
plus.push($("[for=" +this.id + "]").html()); // get the label text
}
});
$("#plus-display").html(plus.join(", "));
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<span id="rata-display-1" class="span text-sottotitolo_dimensione _2 rata checkout">784 €</span>
<br> <br><br>
<div id="plus-display" class="text_piccolo black checkout">PLUS:<strong>place in which display the name of the checkboxes</strong></div>
<br><br><br>
<span id="rata-display-2" class="prezzo-checkout">784 €</span>
<br><br><br>
<form id="plus" name="plus" data-name="plus" class="form-5">
<div class="plus-wrapper interior-pack w-checkbox"><input type="checkbox" id="interior-pack" name="interior-pack" data-name="interior-pack" value="25" class="check-plus w-checkbox-input"><label for="interior-pack" class="checkout-text w-form-label"><strong>Interior Pack<br></strong>+25 € al mese</label></div>
<div class="plus-wrapper domotica w-checkbox"><input type="checkbox" id="domotica" name="domotica" data-name="domotica" value="7" class="check-plus w-checkbox-input"><label for="domotica" class="checkout-text w-form-label"><strong>Sicurezza Domotica<br></strong>+7 € al mese</label></div>
<div class="plus-wrapper security w-checkbox"><input type="checkbox" id="security-plus" name="security-plus" data-name="security-plus" value="9" class="check-plus w-checkbox-input"><label for="checkbox-3" class="checkout-text w-form-label"><strong>Security Plus<br></strong>+9 € al mese</label></div>
<div class="plus-wrapper emotion w-checkbox"><input type="checkbox" id="emotion" name="emotion" data-name="emotion" value="15" class="check-plus w-checkbox-input"><label for="emotion" class="checkout-text w-form-label"><strong>Emotion<br></strong>+15 € al mese</label></div>
<div class="plus-wrapper box-auto w-checkbox"><input type="checkbox" id="box-auto" name="box-auto" data-name="box-auto" value="123" class="check-plus w-checkbox-input"><label for="checkbox-3" class="checkout-text w-form-label"><strong>Box Auto<br></strong>+123 € al mese</label></div>
<div class="plus-wrapper assicurazione w-checkbox"><input type="checkbox" id="assicurazione" name="assicurazione" data-name="assicurazione" value="4" class="check-plus w-checkbox-input"><label for="assicurazione" class="checkout-text w-form-label"><strong>Assicurazione<br></strong>+4 € al mese</label></div>
<div class="plus-wrapper wellness w-checkbox"><input type="checkbox" id="wellness" name="wellness" data-name="wellness" value="45" class="check-plus w-checkbox-input"><label for="wellness" class="checkout-text w-form-label"><strong>Wellness<br></strong>+45 € al mese</label></div>
<div class="plus-wrapper outdoor w-checkbox"><input type="checkbox" id="outdoor" name="outdoor" data-name="outdoor" value="36" class="check-plus w-checkbox-input"><label for="outdoor" class="checkout-text w-form-label"><strong>Outdoor Pack<br></strong>+36 € al mese</label></div>
</form>
答案 0 :(得分:2)
纯JS解决方案:
const form = document.getElementsByTagName("form")[0]
form.addEventListener("click", (e) => { // taking advantage of eventBubbling here
const target = e.target
if(String(target).toLowerCase().includes("input")) { // naive check if clicked element is input - you should implement better check most probably
const clickedCheckboxValue = parseFloat(target.value)
const currentSumElement = document.querySelector("#sum")
const currentSum = parseFloat(currentSumElement.innerText)
currentSumElement.innerText = target.checked
? currentSum + clickedCheckboxValue
: currentSum - clickedCheckboxValue
}
})
此代码何时被触发?您这里没有eventListener
,您的代码中是否有此代码?
很高兴知道如何触发此方法的上下文。
还请详细描述应该发生的情况。
例如
在#someID
上单击获取.someClassName
中的值,并将其添加/减去为#someOtherID
中的值
codepen中的草图:
答案 1 :(得分:2)
我刚刚创建了一个简单的解决方案。可能会更容易。 步骤如下:
请查看代码示例以正确理解。
$('.check-plus').click(function(e){
let sum = 784; // Base Price
$(":checked").each(function(){
// console.log($(this).val());
sum = sum + Number($(this).val());
// console.log("Total Amount :",sum);
});
$('#display').html("Total Amount is : "+sum+"€");
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form id="plus" name="plus" data-name="plus" class="form-5">
<div class="plus-wrapper interior-pack w-checkbox"><input type="checkbox" id="interior-pack" name="interior-pack" data-name="interior-pack" value="25" class="check-plus w-checkbox-input"><label for="interior-pack" class="checkout-text w-form-label"><strong>Interior Pack<br></strong>+25 € al mese</label></div>
<div class="plus-wrapper domotica w-checkbox"><input type="checkbox" id="domotica" name="domotica" data-name="domotica" value="7" class="check-plus w-checkbox-input"><label for="domotica" class="checkout-text w-form-label"><strong>Sicurezza Domotica<br></strong>+7 € al mese</label></div>
<div class="plus-wrapper security w-checkbox"><input type="checkbox" id="security-plus" name="security-plus" data-name="security-plus" value="9" class="check-plus w-checkbox-input"><label for="checkbox-3" class="checkout-text w-form-label"><strong>Security Plus<br></strong>+9 € al mese</label></div>
<div class="plus-wrapper emotion w-checkbox"><input type="checkbox" id="emotion" name="emotion" data-name="emotion" value="15" class="check-plus w-checkbox-input"><label for="emotion" class="checkout-text w-form-label"><strong>Emotion<br></strong>+15 € al mese</label></div>
<div class="plus-wrapper box-auto w-checkbox"><input type="checkbox" id="box-auto" name="box-auto" data-name="box-auto" value="123" class="check-plus w-checkbox-input"><label for="checkbox-3" class="checkout-text w-form-label"><strong>Box Auto<br></strong>+123 € al mese</label></div>
<div class="plus-wrapper assicurazione w-checkbox"><input type="checkbox" id="assicurazione" name="assicurazione" data-name="assicurazione" value="4" class="check-plus w-checkbox-input"><label for="assicurazione" class="checkout-text w-form-label"><strong>Assicurazione<br></strong>+4 € al mese</label></div>
<div class="plus-wrapper wellness w-checkbox"><input type="checkbox" id="wellness" name="wellness" data-name="wellness" value="45" class="check-plus w-checkbox-input"><label for="wellness" class="checkout-text w-form-label"><strong>Wellness<br></strong>+45 € al mese</label></div>
<div class="plus-wrapper outdoor w-checkbox"><input type="checkbox" id="outdoor" name="outdoor" data-name="outdoor" value="36" class="check-plus w-checkbox-input"><label for="outdoor" class="checkout-text w-form-label"><strong>Outdoor Pack<br></strong>+36 € al mese</label></div>
</form>
<h2 id="display">
</h2>
答案 2 :(得分:1)
您的输入没有以plus
开头的ID,因此您的选择器在这里有问题。
您可以使用form#plus div[class^=plus-] input
作为选择器。这限制了选择器的范围并避免了冲突。
在您的JavaScript的第一行中,似乎还发生了一些事情:
var currentPrice = basicPrice; // every time
CurrentPrice = basicPrice,
plus = [],
total = 0;
您可能没有必要设置currentPrice
和CurrentPrice
(两个不同的变量)-以及声明前缺少的var
关键字。
希望这会有所帮助,而不会为您编写代码带来乐趣! :)
答案 3 :(得分:1)
我将在复选框上添加一个事件侦听器。因此,每次更改时,您都可以重新计算所需的内容。此外,我还更改了foreach的选择器,因为您没有以加号开头的ID ...希望对您有所帮助。
var basicPrice = 784 ; // This is how we start
$(document).on('change','input[type=checkbox]', getCheck);
function getCheck() {
var currentPrice = basicPrice; // every time
CurrentPrice = basicPrice,
plus = [],
total = 0;
console.log(currentPrice)
$("input[type=checkbox]").each(function(i, el) {
if ($(el).is(":checked")) {
total += parseInt($(el).val());
console.log(total);
// UPDATE:
plus.push($(el).data('name')); // get the label text
}
});
$("#plus-display").html(plus.join(", "));
$('#rata-display-2').html(total);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<span id="rata-display-1" class="span text-sottotitolo_dimensione _2 rata checkout">784 €</span>
<br> <br><br>
<div id="plus-display" class="text_piccolo black checkout">PLUS:<strong>place in which display the name of the checkboxes</strong></div>
<br><br><br>
<span id="rata-display-2" class="prezzo-checkout">784 €</span>
<br><br><br>
<form id="plus" name="plus" data-name="plus" class="form-5">
<div class="plus-wrapper interior-pack w-checkbox"><input type="checkbox" id="interior-pack" name="interior-pack" data-name="interior-pack" value="25" class="check-plus w-checkbox-input"><label for="interior-pack" class="checkout-text w-form-label"><strong>Interior Pack<br></strong>+25 € al mese</label></div>
<div class="plus-wrapper domotica w-checkbox"><input type="checkbox" id="domotica" name="domotica" data-name="domotica" value="7" class="check-plus w-checkbox-input"><label for="domotica" class="checkout-text w-form-label"><strong>Sicurezza Domotica<br></strong>+7 € al mese</label></div>
<div class="plus-wrapper security w-checkbox"><input type="checkbox" id="security-plus" name="security-plus" data-name="security-plus" value="9" class="check-plus w-checkbox-input"><label for="checkbox-3" class="checkout-text w-form-label"><strong>Security Plus<br></strong>+9 € al mese</label></div>
<div class="plus-wrapper emotion w-checkbox"><input type="checkbox" id="emotion" name="emotion" data-name="emotion" value="15" class="check-plus w-checkbox-input"><label for="emotion" class="checkout-text w-form-label"><strong>Emotion<br></strong>+15 € al mese</label></div>
<div class="plus-wrapper box-auto w-checkbox"><input type="checkbox" id="box-auto" name="box-auto" data-name="box-auto" value="123" class="check-plus w-checkbox-input"><label for="checkbox-3" class="checkout-text w-form-label"><strong>Box Auto<br></strong>+123 € al mese</label></div>
<div class="plus-wrapper assicurazione w-checkbox"><input type="checkbox" id="assicurazione" name="assicurazione" data-name="assicurazione" value="4" class="check-plus w-checkbox-input"><label for="assicurazione" class="checkout-text w-form-label"><strong>Assicurazione<br></strong>+4 € al mese</label></div>
<div class="plus-wrapper wellness w-checkbox"><input type="checkbox" id="wellness" name="wellness" data-name="wellness" value="45" class="check-plus w-checkbox-input"><label for="wellness" class="checkout-text w-form-label"><strong>Wellness<br></strong>+45 € al mese</label></div>
<div class="plus-wrapper outdoor w-checkbox"><input type="checkbox" id="outdoor" name="outdoor" data-name="outdoor" value="36" class="check-plus w-checkbox-input"><label for="outdoor" class="checkout-text w-form-label"><strong>Outdoor Pack<br></strong>+36 € al mese</label></div>
</form>
答案 4 :(得分:1)
这可以使用JQuery来实现,但实际上我修改了span
标签以将其与货币分开,以便该值是动态的(但是我仅使用一个结帐值对其进行了测试)。 ..
请确保使用parseInt()
来检索span
标记中的文本,以使其成为 number 。
建议您参考{ {3}}对这个想法有一个更好的概念。
$(".check-plus").on("change",function(){
var basic = 784;
$(".check-plus").each(function(){
if(this.checked){
basic += parseInt(this.getAttribute("value"))
name += this.getAttribute("name")+"<br>"
}
})
$(".checkout.rata").text(basic);
$("#plus-display").html(name)
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<span id="rata-display-1" class="span text-sottotitolo_dimensione _2 rata checkout">784</span><span class='currency'> €</span>
<br> <br><br>
<div id="plus-display" class="text_piccolo black checkout">PLUS:<strong>place in which display the name of the checkboxes</strong></div>
<br><br><br>
<span id="rata-display-2" class="prezzo-checkout">784 €</span>
<br><br><br>
<form id="plus" name="plus" data-name="plus" class="form-5">
<div class="plus-wrapper interior-pack w-checkbox"><input type="checkbox" id="interior-pack" name="interior-pack" data-name="interior-pack" value="25" class="check-plus w-checkbox-input"><label for="interior-pack" class="checkout-text w-form-label"><strong>Interior Pack<br></strong>+25 € al mese</label></div>
<div class="plus-wrapper domotica w-checkbox"><input type="checkbox" id="domotica" name="domotica" data-name="domotica" value="7" class="check-plus w-checkbox-input"><label for="domotica" class="checkout-text w-form-label"><strong>Sicurezza Domotica<br></strong>+7 € al mese</label></div>
<div class="plus-wrapper security w-checkbox"><input type="checkbox" id="security-plus" name="security-plus" data-name="security-plus" value="9" class="check-plus w-checkbox-input"><label for="checkbox-3" class="checkout-text w-form-label"><strong>Security Plus<br></strong>+9 € al mese</label></div>
<div class="plus-wrapper emotion w-checkbox"><input type="checkbox" id="emotion" name="emotion" data-name="emotion" value="15" class="check-plus w-checkbox-input"><label for="emotion" class="checkout-text w-form-label"><strong>Emotion<br></strong>+15 € al mese</label></div>
<div class="plus-wrapper box-auto w-checkbox"><input type="checkbox" id="box-auto" name="box-auto" data-name="box-auto" value="123" class="check-plus w-checkbox-input"><label for="checkbox-3" class="checkout-text w-form-label"><strong>Box Auto<br></strong>+123 € al mese</label></div>
<div class="plus-wrapper assicurazione w-checkbox"><input type="checkbox" id="assicurazione" name="assicurazione" data-name="assicurazione" value="4" class="check-plus w-checkbox-input"><label for="assicurazione" class="checkout-text w-form-label"><strong>Assicurazione<br></strong>+4 € al mese</label></div>
<div class="plus-wrapper wellness w-checkbox"><input type="checkbox" id="wellness" name="wellness" data-name="wellness" value="45" class="check-plus w-checkbox-input"><label for="wellness" class="checkout-text w-form-label"><strong>Wellness<br></strong>+45 € al mese</label></div>
<div class="plus-wrapper outdoor w-checkbox"><input type="checkbox" id="outdoor" name="outdoor" data-name="outdoor" value="36" class="check-plus w-checkbox-input"><label for="outdoor" class="checkout-text w-form-label"><strong>Outdoor Pack<br></strong>+36 € al mese</label></div>
</form>