jquery - 如何使输出对函数做出反应?
问题描述
我有一个总价值输出。当未选中复选框时,我需要它减少。它有效,但不适用于我取消选中该框的功能。
Jquery总值输出:
$(document).ready(function() {
var begin = 720;
$("#yett").val('Total €' + begin.toFixed(2).replace(/\./g, ','));
$("#CalculatorPlacing").on("change", function() {
var prev = $("#yett").val().split('€')[1];
if ($(this).prop('checked') == true) {
total = begin - 300;
$("#yett").val('Total €' + total.toFixed(2).replace(/\./g, ','));
}else{
$("#yett").val('Total €' + begin.toFixed(2).replace(/\./g, ','));
}
});
});
Jquery取消选中该框:
$('#CalculatorTransport').change(function() {
if ($('#CalculatorTransport').is(':checked') == true) { // off
$("#CalculatorPlacing").attr("disabled", true);
$('#CalculatorPlacing').prop('checked', true);
}
});
价值输出:
<div class="SummaryRow">
<h1><input type="yeets" name="_mdjm_event_cost" id="yett" class="mdjm-input-currency required" readonly="readonly" value="" placeholder="Totaal €0.00" style="
height: 100px;
width: 275px;
margin-top: -40px;
color: black;
font-weight: bold;" />
</h1>
</div>
两个复选框:
<div class="switch" onclick="TotaalOfferte()">
<input type="checkbox" name="iCalculatorTransport" id="CalculatorTransport" class="cmn-toggle cmn-toggle-round AutosubmitCalculator" value="0" tabindex="4">
<label for="CalculatorTransport" data-on="JA" data-off="NEE"></label>
</div>
<div class="switch">
<input type="checkbox" name="iCalculatorPlacing" id="CalculatorPlacing" class="cmn-toggle cmn-toggle-round AutosubmitCalculator" value="1" tabindex="5">
<label for="CalculatorPlacing" data-on="JA" data-off="NEE"></label>
</div>
当我取消选中“CalculatorTransport”复选框时,它也会取消选中“CalculatorPlacing”复选框,但我的总值函数不会对其做出反应。有谁知道为什么会这样?
解决方案
如果我理解这个问题是正确的,你可以做类似的事情。有关更多详细信息,请参阅片段中的评论
$(document).ready(function() {
var total = $('#yett').text();
$(".switch").on('change', function(event){
// remove $
total = total.substring(0, total.length-1);
//check if clicked checkbox is checked
if($(this).is(":checked")){
//subtract clicked input by the "subtractby" value (from the HTML)
total = ~~total - ~~($(this).attr('subtractby'));
}
else{
//add to the clicked input by the "subtractby" value (from the HTML)
total = ~~total + ~~($(this).attr('subtractby'));
}
//add back the $
total = total + "$";
//update the text
$('#yett').text(total);
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="SummaryRow">
<h1 id="yett">1000$</h1>
</div>
<div>
<label>-187</label>
<input class="switch" type="checkbox" subtractby="187">
</div>
<div>
<label>-27</label>
<input class="switch" type="checkbox" subtractby="27">
</div>
推荐阅读
- apache-spark - 在大数据上优化 Pyspark UDF
- python - Python 中的 YouTube 类别
- ruby-on-rails - 使用记录作为语言环境的 I18 网址生成器错误
- xpath - 如果有,则只选择前面的第二个标签,或者只选择第一个标签
- jasmine - 有没有办法在融合中发布量角器测试结果并进行概述?
- powerbi - 对最终用户隐藏切片器中的项目 (Power BI)
- python - Buildozer 构建的应用程序崩溃:jnius.jnius.JavaException:发生 JVM 异常:找不到类
- javascript - 使用 querySelectorAll 验证表单时 - 如何选择适当的跨度来输出错误
- terraform - Terraform 错误 - 未设置错误更新服务器
- php - 使用 PHP 和 Laravel 创建计数可视化