【问题标题】:How do I put a string into a textbox (username box) using javascript? [duplicate]如何使用 javascript 将字符串放入文本框(用户名框)? [复制]
【发布时间】:2014-10-27 04:19:18
【问题描述】:

我在 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();

【问题讨论】:

标签: javascript google-chrome login tampermonkey


【解决方案1】:
document.getElementById('username').value = 'MyLogin';
document.getElementById('password').value = password; // I don't care how you get it
document.getElementById('loginbutton').click();

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

【讨论】:

  • jQuery 因为:(1)他可能也在做其他事情,(2)这个答案只适用于最简单的静态页面,(3)他可能已经抓住了一个模板并且最聪明的人使用jQuery 作为基础。
  • @BrockAdams:很公平,但在这种情况下,为什么不$('username').val('MyLogin')?我不是反对 jQuery,而是为了一致性。
  • 这可能有效(简单的静态页面),但 OP 没有提供所需的详细信息。我还没有投票赞成这个答案,因为这个问题几乎是重复的,可能应该关闭,不管。
  • @BrockAdams:可能,是的。同意。
  • ID 是否正确?时间是否正确(即您拨打login()时输入框是否存在)?
猜你喜欢
  • 1970-01-01
  • 2011-01-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多