gpt4 book ai didi

javascript - 阻止 js 值变为负值

转载 作者:行者123 更新时间:2023-11-30 06:59:19 26 4
gpt4 key购买 nike

我有一个增量函数的问题,该函数允许用户按下一个按钮来添加或删除票证。但是,我不希望用户能够进入底片并搞砸一切。有什么办法可以阻止这种情况发生。可在 http://kentishtours.co.uk/destinations/canterbury.php 找到工作演示蓝色的“-”“+”按钮就是我所说的。

谢谢

enter image description here

用于识别输入和递增数字的代码。

var adult = $('#adult'),
child = $('#child'),
total = $('.total'),
value = $('#value'),
increase = $('#increase'),
decrease = $('#decrease'),
increasec = $('#increasec'),
decreasec = $('#decreasec');

var calulate_price = function(a, c){
var p1 = 18,
p2 = 10,
PA = a * p1,
PC, d;

if(a >= 1) PC = c * p2;
else PC = c * p1;

d = PA + PC;

total.text("£ " + d + ".00");
value.val(d);
};

increase.click(function(){
var a = Number(adult.val());
var c = Number(child.val());
adult.val(++a);
calulate_price(a, c);
});
decrease.click(function(){
var a = Number(adult.val());
var c = Number(child.val());
adult.val(--a);
calulate_price(a, c);
});
increasec.click(function(){
var a = Number(adult.val());
var c = Number(child.val());
child.val(++c);
calulate_price(a, c);
});
decreasec.click(function(){
var a = Number(adult.val());
var c = Number(child.val());
child.val(--c);
calulate_price(a, c);
});

最佳答案

decrease.click(function(){
var a = Number(adult.val());
var c = Number(child.val());
if(a>0){
adult.val(--a);
}
calulate_price(a, c);
});

关于javascript - 阻止 js 值变为负值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21006405/

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