简体   繁体   English

使用javascript将1 div内容复制到另一页面div

[英]copy 1 div content to another page div using javascript

hello guys I am developing a form at run time and appending the elements inside div . 大家好,我正在运行时开发一个表单,并将元素附加到div中。

But while coping content of the div to another page it is not showing anything as if nothing got appended . 但是当将div的内容处理到另一页时,它没有显示任何内容,好像没有附加任何内容。 can u tell me what is the problem... 你能告诉我问题是什么吗...

https://jsfiddle.net/a80xyep8 https://jsfiddle.net/a80xyep8

<form>

    <div class="dynamicInput" id="dynamicInput">
        /*all dynamic elements are appended here as per javascript coding . but not showing in another page only static things are showing shuch as
        <input type="text" name="a" /> */
    </div>
    <input type="submit" value="submit_form" onclick="f();" />
</form>

<script>
    var counterText = 0;
    var counterRadioButton = 0;
    var counterCheckBox = 0;
    var counterTextArea = 0;
    var lb = "";
    var name = "";

    function addAllInputs(divName, itemName) {

        var newdiv = document.createElement('div');
        switch (itemName) {
            case 'text':
                lb = prompt("enter label name");
                name = prompt("enter name for div attribute");
                //newdiv.innerHTML = " "+(counterText + 1)+ " "+ lb + " <br><input type='text' name='myInputs[]'>";
                newdiv.innerHTML = " " + lb + " <br><input type='text'>";
                newdiv.setAttribute("id", name);
                newdiv.setAttribute("name", name);
                counterText++;
                document.getElementById(divName).appendChild(newdiv);
                break;
            case 'radio':
                {
                    var n = prompt("How many number of elements do you need");
                    lb = prompt("enter label name");

                    newdiv.innerHTML = " " + lb + "<br>";
                    document.getElementById(divName).appendChild(newdiv);
                    for (var i = 0; i < n; i++) {
                        var newdiv1 = document.createElement('div');
                        lb = prompt("enter label name");
                        name = prompt("enter name for div attribute");
                        newdiv1.innerHTML = " " + lb + "<input type='radio'>";
                        newdiv1.setAttribute("id", name);
                        newdiv1.setAttribute("name", name);
                        counterRadioButton++;
                        document.getElementById(divName).appendChild(newdiv1);
                    }
                    counterRadioButton++;
                    break;
                }

            case 'checkbox':
                var n = prompt("How many number of elements do you need");
                if (n == 1) {
                    lb = prompt("enter label name");
                    name = prompt("enter name for div attribute");
                    newdiv.innerHTML = " " + lb + " <br><input type='checkbox' >";
                    newdiv.setAttribute("id", name);
                    newdiv.setAttribute("name", name);
                    document.getElementById(divName).appendChild(newdiv);
                    counterCheckBox++;
                } else if (n > 1) {
                    var newdiv1 = document.createElement('div');
                    lb = prompt("enter label name");
                    newdiv1.innerHTML = " " + lb;
                    document.getElementById(divName).appendChild(newdiv1);
                    for (var i = 0; i < n; i++) {
                        var newdiv1 = document.createElement('div');
                        lb = prompt("enter label name");
                        name = prompt("enter name for div attribute");
                        newdiv1.innerHTML = " " + lb + " <br><input type='checkbox'>";
                        newdiv.setAttribute("id", name);
                        newdiv.setAttribute("name", name);
                        document.getElementById(divName).appendChild(newdiv1);
                        counterCheckBox++;
                    }
                }

                break;

            case 'textarea':
                lb = prompt("enter label name");
                name = prompt("enter name for div attribute");
                newdiv.innerHTML = " " + lb + " <br><textarea>type here...</textarea>";
                newdiv.setAttribute("id", name);
                newdiv.setAttribute("name", name);
                counterTextArea++;
                document.getElementById(divName).appendChild(newdiv);
                break;


                //document.getElementById(divName).appendChild(newdiv);
            case 'combobox':
                lb = prompt("enter label name");
                name = prompt("enter name for div attribute");
                newdiv.innerHTML = " " + lb + " <br><select id='combo[]'>type here...</select>";
                document.getElementById(divName).appendChild(newdiv);
                var textb = lb;
                var combo = document.getElementById("combo");
                newdiv.setAttribute("id", name);
                newdiv.setAttribute("name", name);
                var option = document.createElement("option");
                option.text = textb.value;
                option.value = textb.value;
                try {
                    combo.add(option, null); //Standard 
                } catch (error) {
                    combo.add(option); // IE only
                }
                textb.value = "";
                document.getElementById(divName).appendChild(newdiv);
        }
    }
</script>

I think you should wait for the DOM to be ready: 我认为您应该等待DOM准备就绪:

<script>
    $( document ).ready( function() {
        document.getElementById("copy").innerHTML=sessionStorage.getItem("page1content");
    } )
</script>

Using you code in this way it worked for me perfectly. 以这种方式使用您的代码对我来说是完美的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM