2014-10-17 134 views
0

我需要填寫HTML表單是這樣的:的JavaScript:使用JavaScript填寫HTML表單並提交

<form action="http://www.example.net/index.php" method="post"> 
<div class="poll"> 
<p class="poll-answer"> 
<label><input type='radio' name='option_id' value='12' />Abc</label> 
</p> 
<p class="poll-answer"> 
<label><input type='radio' name='option_id' value='34' />Def</label> 
</p> 
<input type="hidden" name="poll_id" value="56" /> 
<input type="submit" value="Submit!" /> 
</div> 
</form> 

我需要使用JavaScript來填充它,並將其發送。

我所著:

<script> 
function post(path) { 
    method = "post"; 
    var form = document.createElement("form"); 
    form.setAttribute("method", method); 
    form.setAttribute("action", path); 

    var hiddenField = document.createElement("input"); 
    hiddenField.setAttribute("type", "radio"); 
    hiddenField.setAttribute("name", "option_id"); 
    hiddenField.setAttribute("value", "12"); 

    hiddenField.setAttribute("type", "hidden"); 
    hiddenField.setAttribute("name", "poll_id"); 
    hiddenField.setAttribute("value", "56"); 

    form.submit(); 
} 
post('http://www.example.net/index.php'); 
</script> 

但在回答有沒有數據。我需要發送表格Abc = value='12'。表單操作不在我的域名上。我有a.com,表格是b.com

# nc -l 192.168.1.11 -p 80 
POST /index.php HTTP/1.1 
Host: example.net 
Connection: keep-alive 
Content-Length: 0 
Cache-Control: max-age=0 
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8 
Origin: null 
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.101 Safari/537.36 
Content-Type: application/x-www-form-urlencoded 
Accept-Encoding: gzip,deflate 
Accept-Language: cs-CZ,cs;q=0.8 

我做得不好?

+0

看起來您忘記將隱藏字段添加到表單中,並將表單追加到文檔中。 – wwwmarty 2014-10-17 13:56:40

回答

2

您忘記將隱藏字段附加到表單和表單到文檔中。

<script> 
function post(path) { 
    method = "post"; 
    var form = document.createElement("form"); 
    form.setAttribute("method", method); 
    form.setAttribute("action", path); 

    var hiddenField = document.createElement("input"); 
    hiddenField.setAttribute("type", "radio"); 
    hiddenField.setAttribute("name", "option_id"); 
    hiddenField.setAttribute("value", "12"); 

    form.appendChild(hiddenField); 

    hiddenField = document.createElement("input"); 
    hiddenField.setAttribute("type", "hidden"); 
    hiddenField.setAttribute("name", "poll_id"); 
    hiddenField.setAttribute("value", "56"); 

    form.appendChild(hiddenField); 
    document.body.appendChild(form); 

    form.submit(); 
    } 
post('http://www.example.net/index.php'); 
</script> 
+1

即使屬性發生更改,您也會追加兩次相同的字段對象。第二次創建一個新的輸入元素。 – 2014-10-17 14:00:43

+0

如果我用您的代碼打開html頁面,則不會發送到服務器。我查看'/ var/log/apache/access.log'和'/ var/log/apache/error.log',並且沒有任何消息:-​​(有什麼不好嗎? – martin 2014-10-17 14:33:42

+0

Chrome'inspect element' show me'Uncaught TypeError:undefined不是函數file.html:26path file.html:26(anonymous function)' – martin 2014-10-17 14:38:21

0

您已將表單和字段添加到文檔中。您需要將該表單添加到文檔中,並將該表單添加到表單中:

var form = document.createElement("form"); 
form.setAttribute("method", method); 
form.setAttribute("action", path); 

var hiddenField = document.createElement("input"); 
// bla bla 

form.appendChild(hiddenField); 
document.body.appenChild(form);