Greasemonkey:设置表单值在Firefox中不起作用

时间:2010-08-16 01:55:10

标签: firefox forms google-chrome greasemonkey

这在Chrome下完美运行,但在Firefox中,字段不会填充,直到我在页面上点击提交的第二次(从第一次告诉我无效的用户/通过后回来)。我错过了什么?

这是我的Greasemonkey脚本代码:

document.getElementById('loginBtn').addEventListener('click',
function (event) {
    document.getElementById('serverLogin').selectedIndex = "2";
    document.getElementById('usernameLogin').value = "username";
    document.getElementById('passwordLogin').value = "password";
    document.getElementById('loginForm').action = 'urltosubmit';
    location.href="javascript:(function(){ document.forms['loginForm'].submit(); })()";
},true);

2 个答案:

答案 0 :(得分:0)

您应该发布您的表单代码,但问题很可能就是事件传播。

将功能更改为:

document.getElementById('loginBtn').addEventListener('click',
function (event) {
    document.getElementById('serverLogin').selectedIndex = "2";
    document.getElementById('usernameLogin').value = "username";
    document.getElementById('passwordLogin').value = "password";
    document.getElementById('loginForm').action = 'urltosubmit';
    location.href="javascript:(function(){ document.forms['loginForm'].submit(); })()";

    event.preventDefault();
    event.stopPropagation();
    return false;
},true);

答案 1 :(得分:0)

或者也许......

document.getElementById('loginForm').addEventListener("submit", function(e)
{
    var fields = e.target.elements;
    fields.namedItem("serverLogin").selectedIndex = 2;
    fields.namedItem("usernameLogin").value = "username";
    fields.namedItem("passwordLogin").value = "password";
    e.target.setAttribute("action", "urltosubmit");
}, false);