How to create a form dynamically using JavaScript?

前端 未结 3 1156
故里飘歌
故里飘歌 2020-12-05 02:51

I want to create a invisible form anywhere into a HTML page dynamically using JavaScript and then submit automatically.
I want to create the form given below:

         


        
3条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-05 03:47

    You're adding the form element as a child of the text box.

    my_tb.appendChild(my_form);
    

    Should be

    my_form.appendChild(my_tb);
    

    Also, I don't see where you're trying to create the hidden elements, but it's the same thing as adding a text box.

    Another problem - trying to reference the form as document.xxx means that xxx is the name of the form. But anyway, try

    my_form=document.createElement('FORM');
    my_form.name='myForm';
    my_form.method='POST';
    my_form.action='http://www.another_page.com/index.htm';
    
    my_tb=document.createElement('INPUT');
    my_tb.type='TEXT';
    my_tb.name='myInput';
    my_tb.value='Values of my Input';
    my_form.appendChild(my_tb);
    
    my_tb=document.createElement('INPUT');
    my_tb.type='HIDDEN';
    my_tb.name='hidden1';
    my_tb.value='Values of my hidden1';
    my_form.appendChild(my_tb);
    document.body.appendChild(my_form);
    my_form.submit();
    

提交回复
热议问题