我是javascript的菜鸟,想知道当总值低于0时如何显示警告信息。有问题的页面在这里(http://cashmoneypaid.com/order-form-phones-work )我尝试过的脚本如下。非常感谢你的帮助。
function checkTotal() {
document.listForm.total.value = '';
var sum = 20;
for (i = 0; i < document.listForm.choice.length; i++) {
if (document.listForm.choice[i].checked) {
sum = sum + parseInt(document.listForm.choice[i].value);
}
}
document.listForm.total.value = sum;
}
$("#total").ready(function() {
if (sum > 0) {
alert("We are sorry, but at this time we can not offer you any money for your item");
}
});
答案 0 :(得分:0)
由于您使用jQuery
更改了所有内容!!
$(function(){
var sum = 20;
$('form input[type="checkbox"]').change(function(){
if (this.checked)
sum -= parseFloat(this.value);
else
sum += parseFloat(this.value);
if (sum < 0)
alert('bla bla bla');
});
});
你也可以使用这个技巧:
$('form input[type="checkbox"]').change(function(){
var value = parseFloat(this.value) * (this.checked ? -1 : 1);
sum += value
...