好吧,我为此拉头发。我有一个主要由下拉选择和复选框组成的表单-这些都分配有值,以便用户选择事物时,将计算出所有事物的总和值。
我的最后一个输入字段是一个名为“折扣代码”的文本输入,我希望能够输入一个设置代码,并从总和中减去固定的金额。
以下是我用来检测折扣代码是否正确的脚本:
$(".apply_discount").click(function() {
if ($("input[name='discount']").val() === "DISTR50") {
$("span").text("Validated...").show();
$("input[name='discount']").attr("value",500.99);
return true;
}
$("span").text("Not a valid discount code").show().fadeOut(2000);
return false;
});
这是计算我的输入总和的代码:
//iterate through each textboxes and add keyup
//handler to trigger sum event
$(".txt, .select, .checkbox").each(function() {
$(this).change(function(){
calculateSum();
});
});
});
function calculateSum() {
var discount = $("input[name='discount']").attr('value');
var sum = 0;
//iterate through each textboxes and add the values
$(".txt, .select, .checkbox:checked").each(function() {
//add only if the value is number
if(!isNaN(this.value) && this.value.length!=0) {
sum += parseFloat(this.value);
}
});
//.toFixed() method will roundoff the final sum to 2 decimal places
var calc_total = sum;
$("#sum").html(calc_total.toFixed(2));
}
如您所见,总和被附加到一个名为#sum的div上-我不知道如何减去折扣输入的值。
您可以在此处查看我的代码:http : //www.samskirrow.com/projects/distr/index3.html
它在JSfiddle中(尽管计算里程部分不起作用)http://jsfiddle.net/bZhK4/
希望我理解了您要查找的内容:无法将声明calc_total为的calc sum函数更改为:
var calc_total = sum - parseFloat(discount);
然后确保您在应用折扣功能上对其进行了更新
$(".apply_discount").click(function() {
if ($("input[name='discount']").val() === "DISTR50") {
$("span").text("Validated...").show();
$("input[name='discount']").attr("value",500.99);
calculateSum();
return true;
}
$("span").text("Not a valid discount code").show().fadeOut(2000);
return false;
});
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句