且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用javascript创建和提交表单

更新时间:2023-02-15 15:27:59

问题是createElement会不接受HTML。它接受标记名并返回DOM元素。然后,您可以将此元素的value属性设置为您需要的值。

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();
}