我已经为用户创建了一个功能,可以根据所选时间选择一个时段并获得折扣,但现在问题是当用户选择第一个然后选择第二个选项时,金额会被扣除而不是重新计算原价而不是降价
这是我的工作代码
$(document).ready(function(e) {
$('.time-slot').on('click', function() {
$('.time-slot').removeClass('active1');
$(this).addClass('active1');
var dt_time = $(this).attr('data-dt-time');
var time_off = $(this).attr('data-timeoff');
var cost = $('.price').val();
if(time_off !== undefined) {
if($('.time-slot').hasClass('active1')) {
var subtr = parseInt(cost) / 100 * parseInt(time_off);
var subtotal = parseInt(cost) - subtr;
$('.price').val(subtotal);
} else {
var subtr = parseInt(cost) / 100 * parseInt(time_off);
var subtotal = parseInt(cost) + subtr;
$('.price').val(subtotal);
}
}
});
});
li {
cursor:pointer;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<ul>
<li class="time-slot" data-dt-time="08:00" data-timeoff="10">08:00</li>
<li class="time-slot" data-dt-time="08:30" data-timeoff="15">08:30</li>
<li class="time-slot" data-dt-time="09:00" data-timeoff="10">09:00</li>
<li class="time-slot" data-dt-time="09:30" data-timeoff="20">09:30</li>
<li class="time-slot" data-dt-time="10:00" data-timeoff="10">10:00</li>
</ul>
<input type="text" class="price" value="350" />
答案 0 :(得分:1)
我得到的解决方案是你压倒价格等级的价值。我在这里使用了一个新的输入类名originalPrice。我从originalPrice获得原始成本价格并将其发送到价格并执行您想要的操作
$(document).ready(function(e) {
$('.time-slot').on('click', function() {
$('.time-slot').removeClass('active1');
$(this).addClass('active1');
var dt_time = $(this).attr('data-dt-time');
var time_off = $(this).attr('data-timeoff');
var cost = $('.originalPrice').val();
if (time_off !== undefined) {
if ($('.time-slot').hasClass('active1')) {
var subtr = parseInt(cost) / 100 * parseInt(time_off);
var subtotal = parseInt(cost) - subtr;
$('.price').val(subtotal);
} else {
var subtr = parseInt(cost) / 100 * parseInt(time_off);
var subtotal = parseInt(cost) + subtr;
$('.price').val(subtotal);
}
}
});
});
&#13;
li {
cursor: pointer;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<ul>
<li class="time-slot" data-dt-time="08:00" data-timeoff="10">08:00</li>
<li class="time-slot" data-dt-time="08:30" data-timeoff="15">08:30</li>
<li class="time-slot" data-dt-time="09:00" data-timeoff="10">09:00</li>
<li class="time-slot" data-dt-time="09:30" data-timeoff="20">09:30</li>
<li class="time-slot" data-dt-time="10:00" data-timeoff="10">10:00</li>
</ul>
<input type="text" class="originalPrice" value="350" />
<input type="text" class="price" value="350" />
&#13;
答案 1 :(得分:0)
在行
之后$(document).ready(function(e) {
添加变量声明
var cost = 350
并在计算中使用它,而不是直接从价格字段中获取成本。