我想使用JavaScript動態地在HTML頁面的任何位置創建一個不可見的表單,然後自動提交。
我想創建如下形式:如何使用JavaScript動態創建表單?
<form name='myForm' method='post' action='http://www.another_page.com/index.htm'>
<input type='text' name='myInput' value='Values of my input'>
<input type='hidden1' value='Hidden value 1'>
<input type='hidden2' value='Hidden value 2'>
</form>
我試着用下面的JavaScript:
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_tb.appendChild(my_form);
document.body.add(my_form,document.body.elements[0]);
document.my_form.submit();
但不工作?我怎樣才能做到這一點?請幫忙。
也許您應該只使用'.innerHTML' ... – kennytm 2010-10-21 19:38:15
.innerHTML的問題是當您嘗試提交表單時。您不會有對錶單的引用,並且它可能尚未添加到DOM。 – 2010-10-21 19:42:13