Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/google-chrome/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用javascript将字符串放入文本框(用户名框)?_Javascript_Google Chrome_Login_Tampermonkey - Fatal编程技术网

如何使用javascript将字符串放入文本框(用户名框)?

如何使用javascript将字符串放入文本框(用户名框)?,javascript,google-chrome,login,tampermonkey,Javascript,Google Chrome,Login,Tampermonkey,我在chrome上使用Tampermonkey并在网站上运行它。我正在尝试使用登录凭据登录。我让它点击注销,而不是登录。这是我的全部代码: // ==UserScript== // @name Blah // @namespace Blah.com // @description . // @include http://Blah.com/* // @require http://ajax.googleapis.com/ajax/li

我在chrome上使用Tampermonkey并在网站上运行它。我正在尝试使用登录凭据登录。我让它点击注销,而不是登录。这是我的全部代码:

// ==UserScript==
// @name           Blah
// @namespace      Blah.com
// @description   .
// @include        http://Blah.com/*
// @require        http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js
// ==/UserScript==

//function logOut()

//{
    // Log out of Blah 
//  Blah = document.getElementsByName(“Blah");
//  for i in 1..2 do
//  log in
//  //username = Username1
//  password = Password1
//  {
//      {
        //  greyButtons.click();
//      }
//  }
//}

function logOut()
{
    // Log out of Blah incase already signed in
    document.getElementById('Blah').click();
}

logOut();

function logIn()
{
    document.getElementById("Header_Login_tbUsername")
    //I don't know what to put here
}

logIn();

另外,如果你想完全使用纯JS,为什么还要使用jQuery?

jQuery,因为:(1)他可能也在做其他事情,(2)这个答案只适用于最简单的静态页面,(3)他可能已经抓取了一个模板,而最聪明的人使用jQuery作为基础。@BrockAdams:很公平,但在这种情况下为什么不
$('username')。val('MyLogin')
?我不反对jQuery,但出于一致性考虑。这可能有效(简单的静态页面),但OP没有提供所需的详细信息。我还没有对这个答案进行投票,因为这个问题几乎是重复的,可能应该关闭,不必担心。@BrockAdams:可能,是的。同意。ID正确吗?时间正确吗(即,在调用
login()
)时是否存在输入框?另请参阅AJAX驱动的页面(也适用于静态页面)。
document.getElementById('username').value = 'MyLogin';
document.getElementById('password').value = password; // I don't care how you get it
document.getElementById('loginbutton').click();