复选框和文本框值的jQuery总和
获取点击事件的复选框值总和工作正常。但我希望相应地改变结果。我更改了文本框值。
此处示例:http://jsfiddle.net/Bhuro/uszwqLra/
<table border="1">
<tr>
<td>10
<input type="checkbox" class="tot_amount" value="10" id="filled-in-box1" onclick="chaked1();">
<input id="one" type="text">
</td>
</tr>
<tr>
<td>20
<input type="checkbox" class="tot_amount" value="20" id="filled-in-box2" onclick="chaked2();">
<input id="two" type="text">
</td>
</tr>
<tr>
<td>Total
<input type="text" id="total1" readonly>
</td>
</tr>
</table>
JS
<script>
function chaked1(){
$("#filled-in-box1").click(function(event)
{
if(this.checked)
{
document.getElementById("one").value=document.getElementById("filled-in-box1").value;
document.getElementById('one').readOnly = false;
}
else
{
document.getElementById("one").value="0";
document.getElementById('one').readOnly = true;
}
});
}
</script>
<script>
function chaked2(){
$("#filled-in-box2").click(function(event)
{
if(this.checked)
{
document.getElementById("two").value=document.getElementById("filled-in-box2").value;
document.getElementById('two').readOnly = false;
}
else
{
document.getElementById("two").value="0";
document.getElementById('two').readOnly = true;
}
});
}
</script>
<script>
$(".tot_amount").click(function(event) {
var total = 0;
$(".tot_amount:checked").each(function()
{
total += parseInt($(this).val());
});
if (total == 0) {
$('#total1').val('');
}
else {
$('#total1').val(total);
}
});
</script>
答案 0 :(得分:2)
您可以在文本输入上定义keyup
事件,并计算相对于输入值的总数。
以下是它应该如何完成,但你真的需要一个代码重构...
$('input[type=text]').keyup(function(){
var total = 0;
$(".tot_amount:checked").each(function()
{
total += parseInt($(this).next().val());
});
if (total == 0) {
$('#total1').val('');
}
else {
$('#total1').val(total);
}
});
function chaked1(checkbox){
if(checkbox.checked)
{
document.getElementById("one").value=document.getElementById("filled-in-box1").value;
document.getElementById('one').readOnly = false;
}
else
{
document.getElementById("one").value="0";
document.getElementById('one').readOnly = true;
}
calculateTotal();
}
function chaked2(checkbox){
if(checkbox.checked)
{
document.getElementById("two").value=document.getElementById("filled-in-box2").value;
document.getElementById('two').readOnly = false;
}
else
{
document.getElementById("two").value="0";
document.getElementById('two').readOnly = true;
}
calculateTotal();
}
// $(".tot_amount").click(function(event) {
// calculateTotal();
// });
$('input[type=text]').keyup(function(){
calculateTotal();
});
function calculateTotal()
{
var total = 0;
$(".tot_amount:checked").each(function()
{
total += (parseInt($(this).next().val()) == undefined) ? 0 : parseInt($(this).next().val());
});
if (total == 0) {
$('#total1').val('');
}
else {
$('#total1').val(total);
}
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table border="1">
<tr><td>10<input type="checkbox" class="tot_amount" value="10" id="filled-in-box1" onclick="chaked1(this);"><input id="one" type="text" ></td></tr>
<tr><td>20<input type="checkbox" class="tot_amount" value="20" id="filled-in-box2" onclick="chaked2(this);"><input id="two" type="text" ></td></tr>
<tr><td>Total<input type="text" id="total1" readonly></td></tr>
</table>
答案 1 :(得分:0)
此处此代码显示您想要的确切结果。如果取消选中复选框,则会指定默认值。
使用它:
<script>
$(".tot_amount").click(function(event) {
var total = 0;
$(".tot_amount:checked").each(function()
{
var inputVal = $(this).parent().find('input[type="text"]').val();
if(inputVal != ''){
total += parseInt(inputVal);
}else{
total += parseInt($(this).val());
$(this).parent().find('input[type="text"]').val($(this).val());
}
});
if (total == 0) {
$('#total1').val('');
}
else {
$('#total1').val(total);
}
});
</script>