我的增量功能有问题,允许用户按下按钮添加或删除票证。但是我不希望用户能够进入负面并将所有内容搞砸。有什么方法可以阻止这种情况发生。可以在http://kentishtours.co.uk/destinations/canterbury.php找到工作演示,蓝色“ - ”“+”按钮就是我所说的。
由于
用于识别输入并增加数字的代码。
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);
});
答案 0 :(得分:5)
你可以做几件事:
只有3个可能的选项,我敢打赌还剩下更多。 ;)
注意,你总是想检查输入服务器端,特别是涉及金钱时:)
答案 1 :(得分:5)
decrease.click(function(){
var a = Number(adult.val());
var c = Number(child.val());
if(a>0){
adult.val(--a);
}
calulate_price(a, c);
});
答案 2 :(得分:3)
如果value == 0
decrease = $('#decrease');
//set inital state
if(Number(adult.val())==0){
$('#decrease').attr("disabled", "disabled");
}else{
$('#decrease').removeAttr("disabled");
}
increase.click(function(){
var a = Number(adult.val());
adult.val(++a);
//make sure its enabled any time we're > 0
if(a>0){
$('#decrease').removeAttr("disabled");
}
var c = Number(child.val());
calulate_price(a, c);
});
decrease.click(function(){
var a = Number(adult.val());
adult.val(--a);
//disable if we are back to 0
if(a==0){
$('#decrease').attr("disabled", "disabled");
}
var c = Number(child.val());
calulate_price(a, c);
});
答案 3 :(得分:0)
只需设置var number
或其他用于存储您要检查的号码的其他内容。然后,只需使用if来检查该值是否小于零。
if (number < 0)
{
//not correct
}
else
{
//correct
}