1 回答
TA贡献1770条经验 获得超3个赞
select-box您可以简单地获取和的值<h4>并将它们相加,并将总价值放入您的 div 和隐藏的输入中。IE :
$(document).ready(function() {
//on change of slect
$("#priceadd").on('change', function() {
//get select value
var optionText = parseInt($(this).val());
//get text inside h4
var realprice = parseInt($("#prictot").text());
//add
var total =optionText + realprice;
//add total to div nd inputs
$("#prictot").text(total)
$("#price").val(total)
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="col-12 col-sm-9 col-md-9 col-lg-8">
<select class="form-control" id="priceadd">
<option value="0">Not Required</option>
<option value="50">Before Care only(6am to 9am)</option>
<option value="60">After Care Only (5pm to 7pm)</option>
<option value="110">Both</option>
</select>
</div>
<h4 class="price" id="prictot">110</h4>
<form action="{{ route('/payment') }}" id="theForm" method="POST">
@csrf
<input type="text" id="price" name="price" value="110"><br>
<button type="submit" class="btn radius-xl text-uppercase">Buy This Courses</button>
</form>
<h4>如果您需要在其值发生更改之前获取内部已经存在的总数,您可以向<h4>标记添加一个自定义属性,该属性将具有以前的值。IE :
$(document).ready(function() {
//on change of slect
$("#priceadd").on('change', function() {
//get old value
var old_value = parseInt($("#prictot").attr('total-price'))
//get select value
var optionText = parseInt($(this).val());
//add
var total =optionText + old_value;
//add total to div nd inputs
$("#prictot").text(total)
$("#price").val(total)
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="col-12 col-sm-9 col-md-9 col-lg-8">
<select class="form-control" id="priceadd">
<option value="0">Not Required</option>
<option value="50">Before Care only(6am to 9am)</option>
<option value="60">After Care Only (5pm to 7pm)</option>
<option value="110">Both</option>
</select>
</div>
<!--added custom attribute-->
<h4 class="price" total-price="110" id="prictot">110</h4>
<form action="{{ route('/payment') }}" id="theForm" method="POST">
@csrf
<input type="text" id="price" name="price" value="110"><br>
<button type="submit" class="btn radius-xl text-uppercase">Buy This Courses</button>
</form>
- 1 回答
- 0 关注
- 86 浏览
添加回答
举报