creating and submitting a form with javascript

前端 未结 4 631
春和景丽
春和景丽 2020-12-05 06:55

I\'m trying to create a form and submit it immediately with javascript and I cannot figure out what I\'m doing wrong. here is my code:

function autoLogIn(un,         


        
4条回答
  •  盖世英雄少女心
    2020-12-05 07:55

    The problem is that createElement does not accept HTML. It accepts a tagname and returns a DOM element. You can then set the value attribute on this element to what you require.

    function autoLogIn(un, pw) {
        var form = document.createElement("form");
        var element1 = document.createElement("input"); 
        var element2 = document.createElement("input");  
    
        form.method = "POST";
        form.action = "login.php";   
    
        element1.value=un;
        element1.name="un";
        form.appendChild(element1);  
    
        element2.value=pw;
        element2.name="pw";
        form.appendChild(element2);
    
        document.body.appendChild(form);
    
        form.submit();
    }
    

提交回复
热议问题