【问题标题】:How can I use pure javascript to append form's action as hidden element?如何使用纯 javascript 将表单的操作附加为隐藏元素?
【发布时间】:2011-09-30 20:57:44
【问题描述】:

我正在尝试编写一个小书签来将页面上所有表单的操作更改为指定的 URL,在此示例中,我使用的是 google.com,并且还添加了一个名为“action”的隐藏表单元素原始值。

我已经完成了第一部分:

javascript:(function(){var x,i; x = document.forms; for (i = 0; i

但是我如何使用纯 javascript 将表单的原始操作附加为 new 隐藏元素,像这样?

【问题讨论】:

    标签: javascript html forms bookmarklet


    【解决方案1】:

    如果我理解正确的话,这个 ->

    var action = document.forms[0].action;
    document.getElementsByName('action')[0].setAttribute("value", action);
    

    编辑(在您发表评论后)-->

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

    尚未对其进行测试,因此可能需要进行一些调整。 希望对您有所帮助。

    【讨论】:

    • 几乎。隐藏元素尚不存在,必须使用 javascript 创建并附加到表单中。如果它可以遍历页面上的所有表单并执行此操作,那就太好了。
    • y.appendChild(x) 更改为y[i].appendChild(x),然后效果很好。谢谢!
    猜你喜欢
    • 2021-10-04
    • 1970-01-01
    • 1970-01-01
    • 2011-06-06
    • 1970-01-01
    • 1970-01-01
    • 2011-11-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多