如何使用纯javascript附加表单';作为隐藏元素的行为?

如何使用纯javascript附加表单';作为隐藏元素的行为?,javascript,html,forms,bookmarklet,Javascript,Html,Forms,Bookmarklet,我正在尝试编写一个bookmarklet,将页面上所有表单的操作更改为指定的URL,例如,我使用的是google.com,并使用原始值添加一个名为“action”的隐藏表单元素 我已经完成了第一部分的工作: javascript:(function(){var x,i; x = document.forms; for (i = 0; i < x.length; ++i) x[i].action="http://www.google.com/"; })(); javasc

我正在尝试编写一个bookmarklet,将页面上所有表单的操作更改为指定的URL,例如,我使用的是google.com,并使用原始值添加一个名为“action”的隐藏表单元素

我已经完成了第一部分的工作:

javascript:(function(){var x,i; x = document.forms; for (i = 0; i < x.length; ++i) x[i].action="http://www.google.com/"; })(); javascript:(function(){var x,i;x=document.forms;for(i=0;i新的隐藏元素附加,如下所示

<input type="hidden" name="action" value="http://original-action-url.com" />
如果我理解正确,这个->

var action = document.forms[0].action;
document.getElementsByName('action')[0].setAttribute("value", action);
编辑(在您的评论之后)-->

y=document.forms;
对于(i=0;i
尚未测试,因此可能需要进行一些调整。
希望能有帮助。

差不多了。隐藏元素还不存在,必须使用javascript创建并附加到表单中。另外,如果它能够循环浏览页面上的所有表单并执行它,那将是非常棒的。将
y.appendChild(x)
更改为
y[i]。appendChild(x)
,然后它工作得非常好。非常感谢。
y = document.forms; 
for (i = 0; i < y.length; ++i){
var x = document.createElement("input");
x.setAttribute("name", "hiddenAction" + i);
x.setAttribute("type","hidden");
x.setAttribute("value",y[i].getAttribute("action"));
y[i].appendChild(x); //you said Appended to the form.
}