总和动态生成的费用

时间:2016-11-28 11:54:26

标签: javascript jquery html5

我正在设计费用系统,用户可以在其中定义费用组件和他们的费用。我已设法为用户创建设施,以根据需要定义费用组件和费用金额。但是当我试图添加它们时,即使我试图通过为它们分配类来添加它们,这似乎也没有用。以下是html代码:

<html>
    <head>
        <meta content="text/html; charset=ISO-8859-1" http-equiv="content-type">
        <link rel="stylesheet" href="masterfees.css" />
        <title>feemaster</title>
    <script type="text/javascript" src="ffees.js"></script>
    <script type="text/javascript">
        function totalfixfees(){
            var tffees = 0;
            var cusid_ele = document.getElementsByClassName('ffeetotal');
            for (var i = 0; i < cusid_ele.length; ++i) {
            if (!isNaN(parseFloat(cusid_ele[i].value)) )
            tffees += parseFloat(cusid_ele[i].value);  
            }
            document.getElementById('tffees').value=tffees;
        }
        </script>
    <script></script>
        <style>
        </style>
    </head>
        <body>
<!-- Trigger/Open The Modal for adding fees -->
<button id="myBtn">Add New Fees</button>

<!-- The Modal -->
<div id="myModal" class="modal">

  <!-- Modal content -->
  <div class="modal-content">
    <div class="modal-header">
      <span class="close"><button>&#10006;</button></span>
      <h2>Create Fees</h2>
    </div>
    <div class="modal-body">
    <fieldset>
    <legend>Fixed Fees Component</legend>

    <div id="wrapper">
    <table align='center' cellspacing=2 cellpadding=5 id="data_table" border=1>
    <tr>
    <th>Name</th>
    <th>Amount (in Rs)</th>
    </tr>

    <tr id="row1">
    <td id="ffname_row1">Annual Fees</td>
    <td id="ffamount_row1" class="ffeetotal">1000</td>
    <td>
    <input type="button" id="edit_button1" value="Edit" class="edit" onclick="edit_row('1')">
    <input type="button" id="save_button1" value="Save" class="save" onclick="save_row('1')">
    <input type="button" value="Delete" class="delete" onclick="delete_row('1')">
    </td>
    </tr>

    <tr id="row2">
    <td id="ffname_row2">Medical Fees</td>
    <td id="ffamount_row2" class="ffeetotal">2000</td>
    <td>
    <input type="button" id="edit_button2" value="Edit" class="edit" onclick="edit_row('2')">
    <input type="button" id="save_button2" value="Save" class="save" onclick="save_row('2')">
    <input type="button" value="Delete" class="delete" onclick="delete_row('2')">
    </td>
    </tr>

    <tr id="row3">
    <td id="ffname_row3">Tution Fees</td>
    <td id="ffamount_row3" class="ffeetotal">3000</td>
    <td>
    <input type="button" id="edit_button3" value="Edit" class="edit" onclick="edit_row('3')">
    <input type="button" id="save_button3" value="Save" class="save" onclick="save_row('3')">
    <input type="button" value="Delete" class="delete" onclick="delete_row('3')">
    </td>
    </tr>

    <tr>
    <td><input type="text" id="new_ffname"></td>
    <td><input type="number" id="new_ffamount" class="ffeetotal"></td>
    <td><input type="button" class="add" onclick="add_row();" value="Add Row"></td>
    </tr>

    </table>
    </div>
    <br>
    <label> Fixed Fee Total </label>
    <input name="tffees" type="text" id="tffees" class="n1fees" value="" readonly>
    <button onclick="totalfixfees()">Calculate Total Fees</button>
</div>
    <div class="modal-footer">
      <h3></h3>
    </div>
  </div>

</div>
<script>
// Get the modal
var modal = document.getElementById('myModal');

// Get the button that opens the modal
var btn = document.getElementById("myBtn");

// Get the <span> element that closes the modal
var span = document.getElementsByClassName("close")[0];

// When the user clicks the button, open the modal
btn.onclick = function() {
    modal.style.display = "block";
}

// When the user clicks on <span> (x), close the modal
span.onclick = function() {
    modal.style.display = "none";
}

// When the user clicks anywhere outside of the modal, close it
window.onclick = function(event) {
    if (event.target == modal) {
        modal.style.display = "none";
    }
}
</script>
        </body>
</html> 

javascript是

function edit_row(no)
{
 document.getElementById("edit_button"+no).style.display="none";
 document.getElementById("save_button"+no).style.display="block";

 var ffname=document.getElementById("ffname_row"+no);
 var ffamount=document.getElementById("ffamount_row"+no);

 var ffname_data=ffname.innerHTML;
 var ffamount_data=ffamount.innerHTML;

 ffname.innerHTML="<input type='text' id='ffname_text"+no+"' value='"+ffname_data+"'>";
 ffamount.innerHTML="<input type='number' class='ffeetotal' id='ffamount_text"+no+"' value='"+ffamount_data+"'>";
}

function save_row(no)
{
 var ffname_val=document.getElementById("ffname_text"+no).value;
 var ffamount_val=document.getElementById("ffamount_text"+no).value;

 document.getElementById("ffname_row"+no).innerHTML=ffname_val;
 document.getElementById("ffamount_row"+no).innerHTML=ffamount_val;


 document.getElementById("edit_button"+no).style.display="block";
 document.getElementById("save_button"+no).style.display="none";
}

function delete_row(no)
{
 document.getElementById("row"+no+"").outerHTML="";
}

function add_row()
{
 var new_ffname=document.getElementById("new_ffname").value;
 var new_ffamount=document.getElementById("new_ffamount").value;

 var table=document.getElementById("data_table");
 var table_len=(table.rows.length)-1;
 var row = table.insertRow(table_len).outerHTML="<tr id='row"+table_len+"'><td id='ffname_row"+table_len+"'>"+new_ffname+"</td><td id='ffamount_row"+table_len+"'>"+new_ffamount+"</td><td><input type='button' id='edit_button"+table_len+"' value='Edit' class='edit' onclick='edit_row("+table_len+")'> <input type='button' id='save_button"+table_len+"' value='Save' class='save' onclick='save_row("+table_len+")'> <input type='button' value='Delete' class='delete' onclick='delete_row("+table_len+")'></td></tr>";

 document.getElementById("new_ffname").value="";
 document.getElementById("new_ffamount").value="";

}

我不确定我哪里出错了。你能帮我吗?

由于

2 个答案:

答案 0 :(得分:1)

您正在阅读value属性以检索费用,但这些元素不是input元素,而是td元素,它们不具有value } property。

而是使用textContent

function totalfixfees(){
    var tffees = 0;
    var cusid_ele = document.getElementsByClassName('ffeetotal');
    for (var i = 0; i < cusid_ele.length; ++i) {
      if (!isNaN(parseFloat(cusid_ele[i].textContent)) )
        tffees += parseFloat(cusid_ele[i].textContent);  
    }
    document.getElementById('tffees').value=tffees;
}

确保在插入新行时还将类 ffeetotal 添加到相关的td元素中。因此,相应地更改以下任务:

var row = table.insertRow(table_len).outerHTML="...

..所以它有本部分提到的类:

"... <td id='ffamount_row"+table_len+"' class='ffeetotal'> ..."

答案 1 :(得分:1)

您必须将next类添加到ffeetotal函数中新添加的td。使用此fiddle

JS:

add_row