简体   繁体   English

JavaScript - 将新字段添加到现有字段

[英]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.

相关问题 添加新输入字段不会更新现有输入字段的值 - Adding new input field doesn't update the value of existing ones 如何向 JavaScript 中的现有键值添加新键值? - How can I add new key values to existing ones in JavaScript? 如何在添加新文本后使用Javascript保留Textfield的值? - How to keep the Value of a Textfield with Javascript after adding new ones? 向现有的javascript对象添加新属性 - Adding new property to existing javascript object 添加新字段时,如何在现有输入字段中保存值? - How to save the values in existing input fields when adding a new one? javascript将新数组映射到现有数组上,添加新属性 - javascript map a new array onto an existing one adding new property 通过检查现有属性来添加多个新属性 - Add multiple new attributes by checking the existing ones jQuery-Javascript-从隐藏输入字段中删除值在添加新值后再次显示 - Jquery - Javascript - Delete values from hidden input field are displaying again after adding new ones 根据同级的现有属性将新属性添加到javascript对象中 - Adding a new property into a javascript object based on existing property of siblings 向 javascript 中的现有数组添加新键和一组值 - Adding new keys and a set of values to an already existing array in javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM