【问题标题】:Fill a form from another website JS从另一个网站 JS 填写表格
【发布时间】:2018-06-02 20:36:39
【问题描述】:

我想从另一个网站填写表格,但我有一个错误,我不明白为什么。

我的 JS 的功能:

<script type="text/javascript">
  function rempliFormulair(url) {
    var fenetrePopup = window.open(url);
 fenetrePopup.getElementsByName("NameText").value = "MyLogin";
    fenetrePopup.getElementsByName("NameForm").submit();
  }

  </script>

有我的表格:

<form name = "NameForm" class="" action="index.html" method="post">
  <input id = "idText" type="text" name="NameText" value="">
    <input id = "azee" type="submit" name="submit" value="aze">
</form>

我的错误:

未捕获的 TypeError: fenetrePopup.getElementsByName 不是 rempliFormulair 的函数

【问题讨论】:

  • 试试fenetrePopup.document.getElementsByName
  • 感谢您的回答,但没有比这更好的了:/

标签: javascript


【解决方案1】:

未捕获的类型错误:fenetrePopup.getElementsByName 不是函数 在rempliFormulair

fenetrePopup 是对 pop-window 对象的引用。您需要同时访问document 对象。另外你只能访问文档if the origin is same

其次,getElementsByName 返回一个NodeList

试试

fenetrePopup.document.getElementsByName("NameText")[0].value = "MyLogin";
fenetrePopup.document.getElementsByName("NameForm")[0].submit();

或使用querySelector

var doc = fenetrePopup.document;
doc.querySelector("[name='NameText']").value = "MyLogin";
doc.querySelector("[name='NameForm']").submit();

注意

这仅在url 位于同源时才有效。

编辑

还需要等待子windowonload事件

fenetrePopup.onload = function(){
    var doc = fenetrePopup.document;
    doc.querySelector("[name='NameText']").value = "MyLogin";
    doc.querySelector("[name='NameForm']").submit();
};

【讨论】:

  • 我不明白起源是什么。我把你告诉我的,但我有另一个错误:
  • 对不起,我按了 Enter :我的错误是:未捕获的 TypeError:无法在 rempliFormulair 设置未定义的属性“值”
  • @C.attia 这意味着弹出窗口中没有名为 NameText 的元素。请分享弹出窗口的标记。
  • 弹出的是一个名为mypage.php的页面,在这个页面中,我只有一个Form :&lt;form name = "NameForm" class="" action="index.html" method="post"&gt; &lt;input id = "idText" type="text" name="NameText" value=""&gt; &lt;input id = "azee" type="submit" name="submit" value="aze"&gt; &lt;/form&gt;
  • @C.attia 那么它应该可以工作,你能分享console.log(fenetrePopup.document)的输出吗
猜你喜欢
  • 1970-01-01
  • 2011-10-10
  • 1970-01-01
  • 2020-07-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-06
相关资源
最近更新 更多