我真的需要能帮助我的人: - )
我想通过点击我店里的各种复选框给客户打折。
访客可以选择最多4个复选框。不是5,6等可以这样做吗?
我现在的问题是,顾客实际上可以免费获得物品 - 或者更糟糕的是 - 我必须付钱才能向他们出售商品!
这是测试代码:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<input type="checkbox" name="checkbox1" id="checkbox1" class="css-checkbox" value="20" />
<label for="checkbox1" class="css-label">banana</label><br>
<input type="checkbox" name="checkbox2" id="checkbox2" class="css-checkbox" value="20" />
<label for="checkbox2" class="css-label">apple</label><br>
<input type="checkbox" name="checkbox3" id="checkbox3" class="css-checkbox" value="20" />
<label for="checkbox3" class="css-label">biscuit</label><br>
<input type="checkbox" name="checkbox4" id="checkbox4" class="css-checkbox" value="20" />
<label for="checkbox4" class="css-label">jam </label><br>
<input type="checkbox" name="checkbox5" id="checkbox5" class="css-checkbox" value="20" />
<label for="checkbox5" class="css-label">orange </label><br>
<input type="checkbox" name="checkbox6" id="checkbox6" class="css-checkbox" value="20" />
<label for="checkbox6" class="css-label">pinepple </label><br>
<br /><br />
<span id="total"><b>Normal price: </b> 100</span></p>
<script>
var $total = 100;
var $total2 = 100;
$('input:checkbox').on('change', function() {
if (this.checked)
$total += -this.value;
else
$total -= -this.value;
if ($total2 > $total)
$('#total').html('<b>Discount price: </b>'+$total);
else
$('#total').html('<b>Normal price: </b>'+$total);
});
</script>
答案 0 :(得分:0)
所以逻辑是 -
如果单击4个复选框,则禁用休息
var $total = 100;
var $total2 = 100;
$('input:checkbox').on('change', function() {
if($('input:checked').length == 4) {
$('input:checkbox:not(:checked)').attr("disabled", true);
}
else {
$('input:checkbox:not(:checked)').attr("disabled", false);
}
if (this.checked)
$total += -this.value;
else
$total -= -this.value;
if ($total2 > $total)
$('#total').html('<b>Discount price: </b>'+$total);
else
$('#total').html('<b>Normal price: </b>'+$total);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="checkbox" name="checkbox1" id="checkbox1" class="css-checkbox" value="20" />
<label for="checkbox1" class="css-label">banana</label><br>
<input type="checkbox" name="checkbox2" id="checkbox2" class="css-checkbox" value="20" />
<label for="checkbox2" class="css-label">apple</label><br>
<input type="checkbox" name="checkbox3" id="checkbox3" class="css-checkbox" value="20" />
<label for="checkbox3" class="css-label">biscuit</label><br>
<input type="checkbox" name="checkbox4" id="checkbox4" class="css-checkbox" value="20" />
<label for="checkbox4" class="css-label">jam </label><br>
<input type="checkbox" name="checkbox5" id="checkbox5" class="css-checkbox" value="20" />
<label for="checkbox5" class="css-label">orange </label><br>
<input type="checkbox" name="checkbox6" id="checkbox6" class="css-checkbox" value="20" />
<label for="checkbox6" class="css-label">pinepple </label><br>
<span id="total"><b>Normal price: </b> 100</span>