0

我的代码有一个文本字段和一个用 javascript 制作的按钮。当用户单击该按钮时,它会打开一个新窗口到网站的搜索页面。我想做的是使用它的 id 填写搜索字段,然后激活网站的搜索按钮。我似乎无法将文本传递到外部站点的文本字段,但这就是我所拥有的。

<script type="text/javascript">// <![CDATA[
function mySearch()
{


popupWindow = window.open(

'http://www.websitehere.com','popUpWindow','height=768,width=1024,left=10,top=10,resizable=yes,scrollbars=yes,toolbar=no,menubar=no,location=no,directories=no,status=yes')


popupWindow.focus();

popupWindow.searchpath1 = 'test value';


}
// ]]></script>
<center><form><input name="searchTxt" type="text" /><br />&nbsp; <input onclick="mySearch()" value="Search" type="button" /></form></center>

textBoxId 将是新打开的窗口上的搜索文本框的 ID。

如果有什么不明白的,请告诉我。

这是外部站点文本框的源代码:

 <input title="Search Term" type="text" name="searchpath1" id="searchpath1" maxlength="255" size="25" style="width: 300px;" value=""/>
4

2 回答 2

1

尝试类似的东西

popupWindow.document.getElementById('textBoxId').value = 'test value';

代替

popupWindow.textBoxId = 'test value';
于 2013-04-02T22:24:44.390 回答
1

您需要获取输入文本 DOM 元素“textBoxId”并设置此获取元素的“值”属性。

这条线是错误的

popupWindow.textBoxId = 'test value';

使用以下行替换它:

var targetTextField = popupWindow.document.getElementById('textBoxId');
targetTextField.value = "test value";

在这里,我重写了完整的函数定义来评估提出的解决方案。试试看,让我们知道它是如何工作的![]s

function mySearch()
{
    popupWindow = window.open('file:///tmp/form.html','popUpWindow','height=768,width=1024,left=10,top=10,resizable=yes,scrollbars=yes,toolbar=no,menubar=no,location=no,directories=no,status=yes')

    if (window.focus()) 
        popupWindow.focus();

    var targetTextField = popupWindow.document.getElementById('textBoxId');
    targetTextField.value = 'test value';
    targetTextField.focus();
}
于 2013-04-02T22:31:29.887 回答