2015-10-16 67 views
1

hello guys我正在開發一個表單,並在div中添加元素。將1 div內容複製到另一個頁面div使用javascript

但是,當處理div的內容到另一個頁面時,它沒有顯示任何東西,就像沒有附加任何東西。 u能告訴我是什麼問題...

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> 
+0

源代碼.... https://jsfiddle.net/a80xyep8/ –

+0

我正在應對使用這種方法頁1 ...................... .......... }頁 –

+0

2 ....

回答

0

我想你應該等待DOM做好準備:使用你的代碼以這種方式,它的工作

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

對我來說完美。

+0

工作感謝:) –

相關問題