我有一个带有一些复选框和一个Javascript片段的表单,其中复选框值已添加并写入<span>
。这工作正常,但我真的希望它写入输入文本字段而不是<span>
。
以下是我的表单的复选框部分:
<section id="extra-features">
<div class="checkbox">
<label><input type="checkbox" name="checkbox" id="outside" class="sum" value="10" data-toggle="checkbox"> Outside Wash</label>
</div><br/>
<div class="checkbox">
<label><input type="checkbox" name="checkbox" id="aclean" class="sum" value="39" data-toggle="checkbox"> A - Clean: Wash Vacuum, Windows, Wheels/Tires, Wax</label>
</div><br/>
<div class="checkbox">
<label><input type="checkbox" name="checkbox" id="bclean" class="sum" value="0" data-toggle="checkbox"> B - Clean: Same as A above <em>PLUS:</em> Shampoo Interior, Clean/Dress Interior Panels, Remove Bugs/Tar.</label>
</div><br/>
<div class="checkbox">
<label><input type="checkbox" name="checkbox" id="cclean" class="sum" value="109" data-toggle="checkbox"> C - Clean: Same as B above <em>PLUS:</em> Compound Polish Exterior, Clean/Dress Moldings as Needed. </label>
</div>
</section>
这是javascript当前正在写的<span>
:
<span id="payment-total" style="text-decoration:underline;">0</span>
这是javascript:
window.onload=function(){
var inputs = document.getElementsByClassName('sum'),
total = document.getElementById('payment-total');
for (var i=0; i < inputs.length; i++) {
inputs[i].onchange = function() {
var add = this.value * (this.checked ? 1 : -1);
total.innerHTML = parseFloat(total.innerHTML) + add
}
}
}
答案 0 :(得分:2)
你不需要循环。使用jQuery的change()而不是onchange。它有一个隐式循环。同样使用jQuery进行检查输入比定制的JS更可靠。这是一个最小的工作示例:
$(document).ready(function() {
function updateSum() {
var total = 0;
$(".sum:checked").each(function(i, n) {total += parseInt($(n).val());})
$("#total").val(total);
}
// run the update on every checkbox change and on startup
$("input.sum").change(updateSum);
updateSum();
})
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<section id="extra-features">
<div class="checkbox">
<label><input type="checkbox" name="checkbox" id="outside" class="sum" value="10" data-toggle="checkbox"> Outside Wash</label>
</div><br/>
<div class="checkbox">
<label><input type="checkbox" name="checkbox" id="aclean" class="sum" value="39" data-toggle="checkbox"> A - Clean: Wash Vacuum, Windows, Wheels/Tires, Wax</label>
</div><br/>
<div class="checkbox">
<label><input type="checkbox" name="checkbox" id="bclean" class="sum" value="0" data-toggle="checkbox"> B - Clean: Same as A above <em>PLUS:</em> Shampoo Interior, Clean/Dress Interior Panels, Remove Bugs/Tar.</label>
</div><br/>
<div class="checkbox">
<label><input type="checkbox" name="checkbox" id="cclean" class="sum" value="109" data-toggle="checkbox"> C - Clean: Same as B above <em>PLUS:</em> Compound Polish Exterior, Clean/Dress Moldings as Needed. </label>
</div>
</section>
<input type="text" id="total">
&#13;
答案 1 :(得分:1)
使用您的代码:
请参阅:https://msdn.microsoft.com/en-us/library/ms535126(v=vs.85).aspx
window.onload=function(){
var inputs = document.getElementsByClassName('sum'),
total = document.getElementById('payment-total');
for (var i=0; i < inputs.length; i++) {
inputs[i].onchange = function() {
var add = this.value * (this.checked ? 1 : -1);
total.innerHTML = parseFloat(total.innerHTML) + add
var new_total = parseFloat(document.getElementById('input').value);
console.log(new_total);
document.getElementById('input').value=new_total + add
}
}
}
<section id="extra-features">
<div class="checkbox">
<label><input type="checkbox" name="checkbox" id="outside" class="sum" value="10" data-toggle="checkbox"> Outside Wash</label>
</div><br/>
<div class="checkbox">
<label><input type="checkbox" name="checkbox" id="aclean" class="sum" value="39" data-toggle="checkbox"> A - Clean: Wash Vacuum, Windows, Wheels/Tires, Wax</label>
</div><br/>
<div class="checkbox">
<label><input type="checkbox" name="checkbox" id="bclean" class="sum" value="0" data-toggle="checkbox"> B - Clean: Same as A above <em>PLUS:</em> Shampoo Interior, Clean/Dress Interior Panels, Remove Bugs/Tar.</label>
</div><br/>
<div class="checkbox">
<label><input type="checkbox" name="checkbox" id="cclean" class="sum" value="109" data-toggle="checkbox"> C - Clean: Same as B above <em>PLUS:</em> Compound Polish Exterior, Clean/Dress Moldings as Needed. </label>
</div>
</section>
This is the <span> that the javascript is writing to currently:
<span id="payment-total" style="text-decoration:underline;">0</span>
<input id="input" type="text" value="0"/>
答案 2 :(得分:0)
你必须为jQuery做
$(total).val(parseFloat(total.innerHTML) + add);
对于javascript
total.value parseFloat(total.innerHTML) + add;