gpt4 book ai didi

JavaScript:使用 JavaScript 填写 HTML 表单并提交

转载 作者:行者123 更新时间:2023-12-02 16:52:58 26 4
gpt4 key购买 nike

我需要像这样填写 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

我做错了什么?

最佳答案

您忘记将隐藏字段附加到表单并将表单附加到文档。

<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>

关于JavaScript:使用 JavaScript 填写 HTML 表单并提交,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26426616/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com