这个脚本只能从第二次开始运行



我找不到地方修复它。下面的脚本只从第二次开始执行。

jQuery(document).ready(function($) {    
$("input[type=checkbox]").change(function(){
recalculate();
});
var wrapa = $("#infosp");
function recalculate(){
var total = 0;   
function uncheckAll() {
$("input[type='checkbox']:checked").prop("checked", false)
$('#amount').val('');
wrapa.removeClass("fix-search");   
}
$(':button').on('click', uncheckAll)         
$("input[type=checkbox]:checked").each(function(){
total += parseInt($(this).val());
});
if (total == 0) {
$('#amount').val('');
wrapa.removeClass("fix-search");
} 
else {
wrapa.addClass("fix-search");
$('#amount').val(total);
}
}
});

请帮我找出问题出在哪里。

更新:

HTML示例:

<input value="20" type="checkbox" class="my-activity">
<input value="40" type="checkbox" class="my-activity">
<div id="infosp" class="fix-search"> SP need for selected levels: <input type="text" name="amount" id="amount" readonly=""> <input type="button" class="checka" value="Reload"> </div>

可能,您已经在head或infosop之前添加了脚本。只需在正文底部添加脚本即可。

<body>
.
.
.
.
<script>
YOUT CURRENT SCRIPT
</script>
</body>

如果这不起作用,只需设置时间,

setTimeout(function(){
$("input[type=checkbox]").change(function(){
recalculate();
});
var wrapa = $("#infosp");
function recalculate(){
var total = 0;   
function uncheckAll() {
$("input[type='checkbox']:checked").prop("checked", false)
$('#amount').val('');
wrapa.removeClass("fix-search");   
}
$(':button').on('click', uncheckAll)         
$("input[type=checkbox]:checked").each(function(){
total += parseInt($(this).val());
});
if (total == 0) {
$('#amount').val('');
wrapa.removeClass("fix-search");
} 
else {
wrapa.addClass("fix-search");
$('#amount').val(total);
}
}
},2000);

最新更新