类名而不是实时输出的id?

时间:2016-12-08 18:35:53

标签: javascript html forms getelementbyid

我有一个表单,并且当用户在文本字段中输入时,输出可以进一步向下看。目前我正在使用'id',因此输出仅限于一个字段。

我想要做的是在用户输入时在多个文本字段中显示输出。我只是通过编辑我在网上找到的一些代码来实现这一目标,但我不知道如何在尝试了几个小时后从这里开始。任何帮助将非常感激。我把下面的jsbin链接和下面的代码放在一起,以防它无法正常工作。

提前致谢

http://jsbin.com/tugulu/edit?html,js,output

HTML

<html>
    <head>
        <meta charset="utf-8">
        <title>Company</title>
        <link rel="stylesheet" type="text/css" href="style.css">
    </head>
    <body>
        <form action="begin-create-done.html" method="get">
            <fieldset>
                <p><label class="field"> Company Name </label>:
                    <span><input  type="text" name="Company"></span> <br>
                <p><label class="field"> Full Company Name </label>:
                    <span><input  type="text" name="CompanyFull"></span> <br>
                <p><label class="field"> Registration Number </label>:
                    <span><input  type="text" name="CompanyNumber"></span> <br>
                <p><label class="field"> Full Company Address </label>:
                    <span><input  type="text" name="CompanyAddress"></span> <br>
            </fieldset>  
        </form>
        Company = <span id="Company"></span><br>
        Company Full Name = <span id="CompanyFull"></span><br>
        Company Registration Number = <span id="CompanyNumber"></span><br>
        Company Address = <span id="CompanyAddress"></span>
        <script src="input.js"></script>    
    </body>
</html>

fieldset {
    width: 600px;
}

label.field {
    text-align: right;
    width:200px;
    float: left;
    font-weight: bold;
    color: black;
}

input.textbox-300 {
    width: 300px;
    float: left;
}

fieldset p {
    clear: both;
    padding: 5px;
}

的javascript

    // create the object that will hold the input values
var formValues = {};
function inputObj(formNR, defaultValues) { 
    var inputs = formNR.getElementsByTagName('input');
    for ( var i = 0; i < inputs.length; i++) {
        formValues[inputs[i].name] = defaultValues[i];
        if(inputs[i].type === 'text') {
            inputs[i].value = defaultValues[i];                            
            document.getElementById(inputs[i].name).innerHTML = defaultValues[i];
        }
        inputs[i].addEventListener('keyup', function() {
            formValues[this.name] = this.value;
            document.getElementById(this.name).innerHTML = this.value;
        }, false);
    }
}
    // build a little array with the defaultValues for each input
var defValues =[];
    // this will push all inputs from the given form in the formValues object.
inputObj(document.forms[0], defValues); 

1 个答案:

答案 0 :(得分:0)

一位朋友帮助我解决了这个问题,代码如下:

<html>
    <head>
        <meta charset="utf-8">
        <title>Company Details</title>
    </head>

    <style>fieldset {
    width: 600px;
}


label.field {
    text-align: right;
    width:200px;
    float: left;
    font-weight: bold;
    color: black;
}

input.textbox-300 {
    width: 300px;
    float: left;
}

fieldset p {
    clear: both;
    padding: 5px;
}
</style>

    <body>
        <form action="begin-create-done.html" method="get">
            <fieldset>
                <p><label class="field"> Name </label>:
                    <span><input  type="text" name="Name1"></span> <br>
            </fieldset>  
        </form>


        Name 1 = <span class="Name1"></span><br>

        Name 2  = <span class="Name1"></span><br>




        <script>
            var formValues = {};
            function inputObj(formNR, defaultValues) { 
                var inputs = formNR.getElementsByTagName('input');
                for ( var i = 0; i < inputs.length; i++) {
                    formValues[inputs[i].name] = defaultValues[i];
                    if(inputs[i].type === 'text') {
                        inputs[i].value = defaultValues[i];                            
                        outputs = document.getElementsByClassName(inputs[i].name);
                        for ( var j = 0; j < outputs.length; j++) {
                            outputs[j].innerHTML = defaultValues[i];
                        }
                    }
                    inputs[i].addEventListener('keyup', function() {
                        formValues[this.name] = this.value;
                        outputs = document.getElementsByClassName(this.name);
                        for ( var j = 0; j < outputs.length; j++) {
                            outputs[j].innerHTML = this.value;
                        }
                    }, false);
                }
            }
                // build a little array with the defaultValues for each input
            var defValues =[];
                // this will push all inputs from the given form in the formValues object.
            inputObj(document.forms[0], defValues); 
        </script>


    </body>
</html>