Skip to main content

Using jquery.calculation.js with dynamically added form fields



I'm creating a form with dynamically added fields and would like to sum all added fields. So far I have the following code, but it only sums the first row:







<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">

<title>Totals</title>

<script type="text/javascript" src="../js/jquery/jquery-1.6.2.js"></script>

<script type="text/javascript" src="../js/jquery/jquery.calculation.js"></script>



<script type="text/javascript">

//Sum

$(document).ready(

function (){

$('input[name^=reds]').sum("keyup", "#totalSumReds");

$("input[name^=blues]").sum("keyup", "#totalSumBlues");

}

);

</script>



<script type="text/javascript">

//Add/remove form fields

jQuery(function($){

$('#btnAdd').click( function(){

var num = $('.clonedInput').length;

var newNum = new Number(num + 1);

var newElem = $('#input' + num).clone().attr('id', 'input' + newNum);



newElem.children(':first').attr('id', 'name' + newNum).attr('reds', 'reds' + newNum);

$('#input' + num).after(newElem);

$('#btnDel').attr('disabled', false);

if (newNum == 5)

$('#btnAdd').attr('disabled', 'disabled');

});



$('#btnDel').click( function() {

var num = $('.clonedInput').length;

$('#input' + num).remove();

$('#btnAdd').attr('disabled', false);

if (num-1 == 1 )

$('#btnDel').attr('disabled', 'disabled');

});



$('#btnDel').attr('disabled', 'disabled');

});

</script>

</head>



<body>

<form id="myForm">

<div id="input1" style="margin-bottom:4px;" class="clonedInput">

Numbers: <input type="text" name="reds"/>

<input type="text" name="blues" id="blues"/>

</div>

<div>

Totals: <input type="text" name="totalSumReds" id="totalSumReds" value="" size="2" readonly="readonly" />

<input type="text" name="totalSumBlues" id="totalSumBlues" value="" size="2" readonly="readonly" />



</div>

<div>

<input type="button" id="btnAdd" value="add line" />

<input type="button" id="btnDel" value="remove line" />

</div>

</form>

</body>

</html>







Any ideas about what should I try to fix it? Thanks!


Comments

  1. In your code you are registering the summing function only on the existing input fields, any fields added afterwards won't trigger the calculation and also won't be taken into account when summing.

    You could use event delegation instead:

    <script type="text/javascript">
    // Sum
    $(function (){
    $('#myForm').live("keyup", "input[name^=reds]", function() {
    var sum = $('input[name^=reds]').sum();
    $('#totalSumReds').val(sum);
    });
    $('#myForm').live("keyup", "input[name^=blues]", function() {
    var sum = $('input[name^=blues]').sum();
    $('#totalSumBlues').val(sum);
    });
    });
    </script>

    ReplyDelete

Post a Comment

Popular posts from this blog

Slow Android emulator

I have a 2.67 GHz Celeron processor, 1.21 GB of RAM on a x86 Windows XP Professional machine. My understanding is that the Android emulator should start fairly quickly on such a machine, but for me it does not. I have followed all instructions in setting up the IDE, SDKs, JDKs and such and have had some success in staring the emulator quickly but is very particulary. How can I, if possible, fix this problem?

CCNA 1 Final Exam 2011 latest (hot hot hot)

  Hi! I have been posted content of ccna1 final exam (latest and only question.) I will post the answer and insert image on sunday. If you care, please subscribe your email an become a first person have full test content. Subcribe now  Some question  have not content because this question have images content. So that can you wait for me? SUNDAY 1. A user sees the command prompt: Router(config-if)# . What task can be performed at this mode? Reload the device. Perform basic tests. Configure individual interfaces. Configure individual terminal lines. 2. Refer to the exhibit. Host A attempts to establish a TCP/IP session with host C. During this attempt, a frame was captured with the source MAC address 0050.7320.D632 and the destination MAC address 0030.8517.44C4. The packet inside the captured frame has an IP source address 192.168.7.5, and the destination IP address is 192.168.219.24. At which point in the network was this packet captured? leaving host A leaving ATL leaving...