不久前,在我的一个旧的 weebly 网站上(在我有 javascript 知识之前),我搜索了一种方法来转到只有我知道的页面并自动登录 weebly。
我遇到了下面的代码,它可以满足我的需要(稍作修改)。
<form id="formID" target="frame" method="post" action="[posturl]">
<input type="hidden" name="user" value="__" />
<input type="hidden" name="password" value="__" />
</form>
<iframe id="frame" name="frame" width="800" height="350"></iframe>
<script type="text/javascript">
// submit the form into iframe for login into remote site
document.getElementById('formID').submit();
// once you're logged in, change the source url (if needed)
var iframe = document.getElementById('frame');
*/iframe.onload = function() {
if (iframe.src != "") {
iframe.src = "";
}
}/*
</script>
现在我需要将它用于另一个网站,问题是它需要一个表单 ID,而目标页面/表单没有。
目标页面上的表单代码如下所示:
<form action="[posturl]" method="post" accept-charset="ISO-8859-1">
<table border="0" cellspacing="5" cellpadding="0" class="ssi_table">
<tr>
<td align="left" width="85"><label for="user"><b>Username:</b></label> </td>
<td><input type="text" id="user" name="user" size="9" value="" class="input_text"/></td>
</tr><tr>
<td align="left"><label for="passwrd"><b>Password:</b></label> </td>
<td><input type="password" name="passwrd" id="passwrd" size="9" class="input_password"/></td>
</tr><tr>
<td><input type="hidden" name="cookielength" value="-1"/></td>
</tr>
</table>
<center><input type="submit" value="Login" value="Login" class="button_submit"/></center>
</form>
我的主要问题是,有没有办法在不需要 ID 的情况下识别并提交表单,以及在没有 ID 的情况下如何填写表单?
最佳答案
您可以执行document.forms[..index ..]
。例如,如果它是文档中的第一个表单,则它将是
document.forms[0].submit();
我发现您的表单中还有其他字段。您还可以向上导航到表单。
关于javascript - 在 iframe 中提交和填写没有 ID 的表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31197582/