<pre name="code" class="java">$(function(){</span>
var ids = ["id1","id2","id3"];
$.each(ids,function(){
var flag = $("#"+this);
flag.change(function(){
var lastVal=0;
$.each(ids,function(){
var v = parseInt($("#"+this).val());
if(!isNaN(v)){
lastVal = parseInt(lastVal)+parseInt(v);
}
});
$("#id4").val(lastVal);
});
});
});