0

抱歉标题含糊。我有一个用户脚本,我正在为一个我经常光顾的网站工作。我正在编辑一个被放弃的页面,以使其在客户端再次半工作。我无法向您展示该页面,但我可以向您展示您应该需要的一切。

编辑:我正在尝试更新一个不起作用的表单,我不确定还能说什么。

我的脚本:

var form = document.getElementByTagName("FORM")[0];
var form_new = document.createElement("FORM");
var form_table = document.createElement("TABLE");
var form_submit = document.createElement("INPUT");
var form_input = [{
    "type" : "hidden",
    "name" : "securitytoken",
    "value" : "1384271912-8dbdaf1d1f16d4d6e0a33bf3a1b184c6add5b295"
},{
    "type" : "hidden",
    "name" : "do",
    "value" : "archive"
},{
    "type" : "hidden",
    "name" : "search",
    "value" : "1"
},{
    "type" : "text",
    "name" : "search[phrase]",
    "value" : "",
    "label" : "Shout Contains"
},{
    "type" : "text",
    "name" : "search[username]",
    "value" : "",
    "label" : "Username Contains"
},{
    "type" : "text",
    "name" : "search[time]",
    "value" : "999999999999",
    "label" : "Time"
},{
    "type" : "text",
    "name" : "perpage",
    "value" : "50",
    "label" : "Per Page"
},{
    "type" : "select",
    "name" : "search[sort]",
    "option" : [{
        "value" : "new",
        "text" : "Newest first"
    },{
        "value" : "old",
        "text" : "Oldest first"
    }],
    "label" : "Sort by"
}];

for(var a = 0; a < form_input; a++){
    var tr = document.createElement("TR");
    var td1 = document.createElement("TD");
    var td2 = document.createElement("TD");
    var input = document.createElement("INPUT");
    var select = document.createElement("SELECT");
    var el = form_input[a];

    if(el.type !== "hidden"){
        var td1_text = document.createTextNode(el.label);
        td1.appendChild(td1_text);

        if(el.type == "select"){
            var select_options = el.option;
            for(var b = 0; b < select_options.length; b++){
                var select_option = document.createElement("OPTION");
                select_option.setAttribute("value",select_options[b].value);
                var select_option_text = document.createTextNode(select_options[b].text);
                select_option.appendChild(select_option_text);
                select.appendChild(select_option);
            }
            td2.appendChild(select);
        }else{
            input.setAttribute("type",el.type);
            input.setAttribute("name",el.name);
            input.setAttribute("value",el.value);
            td2.appendChild(input);
        }
        tr.appendChild(td1);
        tr.appendChild(td2);
        form_table.appendChild(tr);
    }else{
        input.setAttribute("type",el.type);
        input.setAttribute("name",el.name);
        input.setAttribute("value",el.value);
        form_new.appendChild(input);
    }
}
form_new.appendChild(form_table);
form.innerHTML = '';
form.setAttribute("style","position:fixed;\
bottom:4px;\
right:4px;\
padding:4px;\
background:#ddd;\
border-radius:10px;\
border:solid 1px black;");
form.setAttribute("method","POST");
form.setAttribute("action","boxoshouts.php?do=archive");
form_submit.setAttribute("type","submit");
form_submit.setAttribute("class","button");
form_submit.setAttribute("value","Submit");
form_new.appendChild(form_submit);
form.appendChild(form_new);

我正在更新的表格:

<form method="post" action="iarchive.php">
<input type="hidden" name="do" value="archive">
<input type="hidden" name="search" value="1">
</form>

你觉得这有什么问题吗?因为我经历了很多次,没有发现任何问题。它在控制台中也没有给出错误,并且 FORM 确实存在,并且是页面上的第一个也是唯一一个。

4

1 回答 1

0

对于初学者:

for(var a = 0; a < form_input; a++){

应该

for (var a = 0; a < form_input.length; a++) {

此外,您不必使用.setAttribute()设置 DOM 元素属性:

        input.type = el.type;
        input.name = el.name;
        input.value = el.value;
于 2013-11-12T17:16:20.080 回答