jqueryif,第一个自由逻辑
好的,所以我编写了一些JQuery,用于在用户添加额外数据时更新price live。有一个代码块用于增压座椅,一个代码块用于儿童座椅 这里有一个,你可以清楚地看到我在做什么:jqueryif,第一个自由逻辑,jquery,Jquery,好的,所以我编写了一些JQuery,用于在用户添加额外数据时更新price live。有一个代码块用于增压座椅,一个代码块用于儿童座椅 这里有一个,你可以清楚地看到我在做什么: $('.boostseat').change(function() { //sets the var id as the id of the select box var id = this.id; //sets this as the value of the s
$('.boostseat').change(function() {
//sets the var id as the id of the select box
var id = this.id;
//sets this as the value of the select box
numberofboostseat = $(this).val();
//shows a confirmation message
$('#boostseatadd-'+id).show();
//this handles if an item is removed, so that the value doesn't reset
if (typeof(boosttotal) != "undefined" && boosttotal !== null) {
price = $("#hiddenprice").val() - boosttotal;
}else{
price = $("#hiddenprice").val();
}
//number of days you hire it for cariable
numofdays = "<?php echo $length->days; ?>";
if (numofdays > 4){
//alert ("boost if1");
boostcost = Number(20);
}else if ((numberofboostseat == 1 || numberofboostseat == 0) && (numberofchildseat == "undefined" || numberofchildseat == 0)){
//alert ("boost if2");
boostcost = Number(3) * Number(0);
}else if (numofdays < 1){
//alert ("boost if3");
boostcost = Number(3) * Number(1);
}else{
//alert ("boost if4");
boostcost = Number(3) * Number(numofdays);
}
// determines final price
boosttotal = Number(numberofboostseat) * Number(boostcost);
//more varaibles
newprice = Number(price) + Number(boosttotal);
//updates hiddden fields
$("input#hiddenboostprice").val(boosttotal);
$("input#hiddenboostnum").val(numberofboostseat);
$("input#hiddenprice").val(newprice);
oldnumofboost = $("input#hiddenboostnum").val(numberofboostseat);
//updates the HTML (Live Price Update) as decimal
$('#'+id).html("€" + newprice.toFixed(2));
});
});
正如您在上面看到的,可变boostcost计算座位的成本。另一段代码,childseat也有全局变量,我可以使用,类似于这些
有人能建议一个if语句,有效地使第一个儿童或安全座椅免费吗?我在变量递减后添加
.discount=true
,以防止姐妹变量be也递减。这样,numberofchildseat或numberofboostseat将以1打折,但决不能两者都打折。只要未设置.discounted
,javascript就会将其计算为false
if(!numberofchildseat.discounted && numberofboostseat > 0){
numberofboostseat--;
numberofboostseat.discounted = true;
}
if(!numberofboostseat.discounted && numberofchildseat > 0){
numberofchildseat--;
numberofchildseat.discounded = true;
}
为什么要使用
Number(x)
?我只是认为确保变量被视为数字是一种安全的方法?我不是JQuery专家……这和JQuery有什么关系?不管怎么说,你用它来表示数字文字,而不是变量。假设一个用户买了3个辅助座椅和5个儿童座椅。1个加高座椅是免费的吗?或者有一个儿童座椅是免费的?一个或两个。如果他们有一个儿童座椅和一个加高座椅。只有他们选择的第一个是免费的。如果这有道理的话。规则是第一个儿童座椅或助推器座椅是免费的。如果这是一个愚蠢的问题,我真的很抱歉,但这一行的号码是儿童座椅--;确切地说是什么?它与foo++
相反。它与numberofchildseat=numberofchildseat-1相同代码>很抱歉,我被从计算机上拖走了,我将在今天早上实现它,并会让您知道。我已经尝试过这个,尽管我认为我理解不正确,当您说您正在添加时。折扣=使用变量后为真?
if(!numberofchildseat.discounted && numberofboostseat > 0){
numberofboostseat--;
numberofboostseat.discounted = true;
}
if(!numberofboostseat.discounted && numberofchildseat > 0){
numberofchildseat--;
numberofchildseat.discounded = true;
}