[英]JavaScript - Adding the new fields to existing ones
I am having a hard time with this homework.我很难完成这个家庭作业。
Trying to keep adding the multiplication of the new 2 fields the user will add.试图继续添加用户将添加的新 2 字段的乘法。
Example:例子:
first field 5 second field 5 = total is 5*5 = 25
第一个字段 5 第二个字段 5 = 总数为 5*5 = 25
But when the user clicks the add field, he would have:但是当用户点击添加字段时,他会:
first field = 5 second field 5
第一个字段 = 5 第二个字段 5
first field = 6 second field 6 total would be 5*5 + 6*6 = 61 and so forth第一个字段 = 6 第二个字段 6 总共是 5*5 + 6*6 = 61 等等
<script>
function calc(form) {
var box1 = parseFloat (form.leftover.value,10)
var box2 = parseFloat (form.charge.value,10)
var temp = new Array();
var Mbalance = ( box1 * box2 ) ;
temp.push(Mbalance) ;
for ( var i=0; i< temp.length ; i++ ) {
Mbalance = Mbalance+Mbalance ;
}
form.mbalance.value= Mbalance ;
}
// This script is identical to the above JavaScript function.
var ct = 1;
function new_link(form) {
ct++ ;
var div1 = document.createElement('div');
div1.id = ct;
// Link to delete extended form elements
div1.innerHTML = document.getElementById('newlinktpl').innerHTML ;
document.getElementById('newlink').appendChild(div1);
}
</script>
</script>
<form>
<div id="newlink">
<div>
<td width="423">What was the balance left over from last month ?</td>
<td width="19">$</td>
<td width="141"><input name="leftover" value="" maxlength="10" /></td>
</tr>
<tr>
<td>How much did you charge this month ?</td>
<td>$</td>
<td><input name="charge" value="" maxlength="10" /></td>
<br/>
</div></div>
<button type="submit" id="buttoncalculate" onclick="calc(this.form); return false; "></button>
<br />
<td width="462">Monthly Balance</td>
<td width="128"><input name="mbalance" value="" maxlength="10" /></td>
<a href="javascript:new_link()">Add new </a>
</form>
<form>
<!-- Template -->
<div id="newlinktpl" style="display:none">
<div>
<td width="423">What was the balance left over from last month ?</td>
<td width="19">$</td>
<td width="141"><input name="leftover" value="" maxlength="10" /></td>
</tr>
<tr>
<td>How much did you charge this month ?</td>
<td>$</td>
<td><input name="charge" value="" maxlength="10" /></td>
</div>
</div>
<form>
Here's one way you can do it.这是您可以做到的一种方法。 It's not necessarily the best, but it achieves what you want and I don't feel like completely rewriting what you had:
它不一定是最好的,但它实现了你想要的,我不想完全重写你所拥有的:
http://jsfiddle.net/HS6dt/2/ http://jsfiddle.net/HS6dt/2/
I added a class to the parent div of your two inputs as well as your two inputs.我向您的两个输入以及您的两个输入的父 div 添加了一个类。 That way I can find the parent div, then find the descendant inputs, get their values and multiple them (note: I have no idea why you are multiplying those two values, it really doesn't make much sense, but whatever):
这样我就可以找到父 div,然后找到后代输入,获取它们的值并将它们相乘(注意:我不知道你为什么要乘以这两个值,这真的没有多大意义,但无论如何):
function calc(form) {
var total = 0;
var items = document.getElementsByClassName("item");
for (var i = 0; i < items.length; i++) {
var left = items[i].getElementsByClassName("leftover");
var charge = items[i].getElementsByClassName("charge");
total += parseFloat(left[0].value, 10) * parseFloat(charge[0].value, 10);
}
form.mbalance.value = total;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.