最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

dom - How to fill in form field, and submit, using javascript? - Stack Overflow

programmeradmin14浏览0评论

If I have an html document whose rough structure is

<html>
<head>
</head>
<body class="bodyclass" id="bodyid">
<div class="headerstuff">..stuff...</div>
<div class = "body">
<form action="" id="login_form" method="post">
<div class="form_section">You can login here</div>
<div class="form_section">
<input xmlns="" class="text" id="username"
       name="session[username_or_email]" tabindex="1" type="text" value="" />
</div>
<div class="form_section">etc</div>
<div xmlns="" class="buttons">
    <button type="submit" class="" name="" id="go" tabindex="3">Go</button>
    <button type="submit" class="" name="cancel" 
            id="cancel" tabindex="4">Cancel</button>
</div>
</form>
</div>
</body>
</html>

You can see that there is a username field and a Go button. How would I, using Javascript, fill in the username and press Go...?

I would prefer to use plain JS, rather than a library like jQuery.

If I have an html document whose rough structure is

<html>
<head>
</head>
<body class="bodyclass" id="bodyid">
<div class="headerstuff">..stuff...</div>
<div class = "body">
<form action="http://example.com/login" id="login_form" method="post">
<div class="form_section">You can login here</div>
<div class="form_section">
<input xmlns="http://www.w3.org/1999/xhtml" class="text" id="username"
       name="session[username_or_email]" tabindex="1" type="text" value="" />
</div>
<div class="form_section">etc</div>
<div xmlns="http://www.w3.org/1999/xhtml" class="buttons">
    <button type="submit" class="" name="" id="go" tabindex="3">Go</button>
    <button type="submit" class="" name="cancel" 
            id="cancel" tabindex="4">Cancel</button>
</div>
</form>
</div>
</body>
</html>

You can see that there is a username field and a Go button. How would I, using Javascript, fill in the username and press Go...?

I would prefer to use plain JS, rather than a library like jQuery.

Share Improve this question edited Jan 13, 2011 at 17:56 DVK 129k33 gold badges217 silver badges334 bronze badges asked Jan 13, 2011 at 17:50 cannyboycannyboy 24.4k40 gold badges150 silver badges256 bronze badges 2
  • Forgive my ignorance, but why are you filling in the xmlns property for both your input and div? That usually belongs in the <html> tag. – user1385191 Commented Jan 13, 2011 at 17:53
  • Any reason why you don't use <input type="submit" />? Your form will not work if JS is disabled. – Felix Kling Commented Jan 13, 2011 at 17:54
Add a comment  | 

5 Answers 5

Reset to default 79
document.getElementById('username').value="moo";
document.forms[0].submit();

Accessed through the document object.

document.getElementById('username').value = 'foo';
document.getElementById('login_form').submit();

You can try something like this:

    <script type="text/javascript">
        function simulateLogin(userName)
        {
            var userNameField = document.getElementById("username");
            userNameField.value = userName;
            var goButton = document.getElementById("go");
            goButton.click();
        }

        simulateLogin("testUser");
</script>

It would be something like:

document.getElementById("username").value="Username";
document.forms[0].submit()

Or similar edit: you guys are too fast ;)

This method helped me doing this task

document.forms['YourFormNameHere'].elements['NameofFormField'].value = "YourValue"
document.forms['YourFormNameHere'].submit();
发布评论

评论列表(0)

  1. 暂无评论