如何使用javascript填写表单字段并提交? [英] How to fill in form field, and submit, using javascript?

查看:32
本文介绍了如何使用javascript填写表单字段并提交?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有一个粗略结构的html文档

<头><body class="bodyclass" id="bodyid"><div class="headerstuff">...东西...</div><div class = "body"><form action="http://example.com/login" id="login_form" method="post"><div class="form_section">您可以在这里登录</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 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">取消</button>

</表单>

您可以看到有一个用户名字段和一个 Go 按钮.我将如何使用 Javascript 填写用户名并按 Go...?

我更喜欢使用纯 JS,而不是像 jQuery 这样的库.

解决方案

document.getElementById('username').value="moo"document.forms[0].submit()

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.

解决方案

document.getElementById('username').value="moo"
document.forms[0].submit()

这篇关于如何使用javascript填写表单字段并提交?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
前端开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆