只是不工作,不知道为什么

时间:2013-11-12 17:11:17

标签: javascript greasemonkey userscripts

对于模糊的标题,我们深表歉意。我有一个我正在为我经常访问的网站工作的用户脚本。我正在编辑一个被遗弃的页面,使其再次成为客户端的半工作。我无法向您展示页面,但我可以向您展示您应该需要的一切。

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

我的剧本:

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确实存在,并且是页面上的第一个也是唯一一个。

1 个答案:

答案 0 :(得分: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;