Javascript 如何使用jQuery打开新窗口并访问新窗口中的元素?

Javascript 如何使用jQuery打开新窗口并访问新窗口中的元素?,javascript,jquery,forms,jquery-selectors,Javascript,Jquery,Forms,Jquery Selectors,我有一个HTML表单,其中有两个带有选项的select元素和两个input元素 <form> <select name="highSchool"> <option value="8342">Clinton High School</option> <option value="9576">Washington High School</option> <opt

我有一个HTML表单,其中有两个带有选项的select元素和两个input元素

<form>
<select name="highSchool">
<option value="8342">Clinton High School</option>
<option value="9576">Washington High School</option>
<option value="8172">Roosevelt High School</option>
<option value="5431">Lincoln High School</option>
<option value="2460">Kennedy High School</option>
</select>

<br><br>

<select name="title">
<option value="1">Amazing Artist</option>
<option value="2">Awesome Athlete</option>
<option value="3">Marvelous Musician</option>
<option value="4">Scholar Student</option>
<option value="5">Wonderful Writer</option>
</select>
<br><br>
<input type="text" name='firstName' size="80" maxlength='30'>
<br><br>
<input type="text" name='lastName' size="80" maxlength='30'>
<br><br>
<button onclick="myFunction1()">Submit</button>
</form>
如何调整所有jQuery选择器,如
$(“输入[name='firstName'])
$(“选择[name='title'])”
,以获取新窗口中的元素?
我对jQuery和Vanilla JS解决方案都持开放态度。

我认为您需要将脚本附加到新窗口,如下所示:

var newWindow = window.open('http://www.exampleweb/example.phtml', windowName, "height=300,width=400");
newWindow.document.createElement('script');
script.src = 'js/myScript.js';
newWindow.document.head.appendChild(script);
var newWindow = window.open('http://www.exampleweb/example.phtml', windowName, "height=300,width=400");
newWindow.document.$("input[name ='firstName']").html('test');
要访问新窗口中的元素,只需引用新窗口的文档,如下所示:

var newWindow = window.open('http://www.exampleweb/example.phtml', windowName, "height=300,width=400");
newWindow.document.createElement('script');
script.src = 'js/myScript.js';
newWindow.document.head.appendChild(script);
var newWindow = window.open('http://www.exampleweb/example.phtml', windowName, "height=300,width=400");
newWindow.document.$("input[name ='firstName']").html('test');
将脚本部分翻译为Vanilla JS,以对newWindow执行完全相同的操作:

var windowName = $(this).attr('id');
var newWindow = window.open('http://www.exampleweb/example.phtml', windowName, "height=300,width=400");
$(newWindow).on("load", function()
    {
      for (var i = 0; i < newWindow.document.querySelector("select[name='highSchool']").options.length; i++){
            var option = newWindow.document.querySelector("select[name='highSchool']").options[i];
            if (option.text === random_dict.highSchool){
                  newWindow.document.querySelector("select[name='highSchool']").value = option.value;
                  break;
            };
      };
            
            
      for (var j = 0; j < newWindow.document.querySelector("select[name='title']").options.length; j++){
            var option1 = newWindow.document.querySelector("select[name='title']").options[j];
            if (option1.text === random_dict.title){
                  newWindow.document.querySelector("select[name='title']").value = option1.value;
                  break;
            };
      };

      newWindow.document.querySelector("input[name='firstName']").value = random_dict.firstName;
      newWindow.document.querySelector("input[name='lastName']").value = random_dict.lastName;
});
var windowName=$(this.attr('id');
var newWindow=window.open('http://www.exampleweb/example.phtml,窗口名,“高度=300,宽度=400”);
$(newWindow).on(“加载”,函数()
{
for(var i=0;i
如果我想从新窗口获取$(“input[name='firstName']),并在当前窗口中对其进行处理,该怎么办?@sweet\u peach2020在vanilla JS中,您将JS脚本附加到哪里?我看不到?我正在打开新窗口,然后执行操作,但没有将其连接到新窗口的头部。这是我原帖中第二段代码的翻译。