<script language="JavaScript">
$(document).ready(function(){
$("#addRow").click(function(){
var html = $(".tramount:last").html();
var last_id = parseInt($(".tramount:last").attr("id").split("_")[1]);
var new_id = last_id+1;
var newRow = '<tr class="tramount" id="tramount_'+new_id+'">';
newRow += html.replace(new RegExp("_"+last_id,"g"), "_"+new_id);
newRow += "</tr>";
$("#tblamount tbody").append(newRow);
});
$("#removeRow").click(function(){
if($(".tramount").length > 1) $(".tramount:last").remove()
});
$('input[name="cost[]"], input[name="amount[]"]').change(function(){
var total = 0.0;
$(".tramount").each(function(){
var now_id = parseInt($(this).attr("id").split("_")[1]);
var cnt = parseInt($("#amount_"+now_id).val());
var price = parseFloat($("#cost_"+now_id).val());
var row = (cnt*price);
if(row!= NaN && row>0) total += row;
});
$("#sum").val(total);
});
});
</script>
โค้ด การ show/hide textbox
<script language="javascript">
$(document).ready(function() {
$('#rentPrice').hide(); //hide field on start
$('#type_1').change(function() {
var $index = $('#type_1').index(this);
if($('#type_1').val() == 'เช่า') { //if this value is NOT selected
$('#rentPrice').show(); //this field is hidden
}
else {
$('#rentPrice').hide();//else it is shown
}
});
});
</script>