通过id javascript填充文本字段

filling in textfield by id javascript

本文关键字:文本 字段 填充 javascript id 通过      更新时间:2023-09-26

我有代码,有一个文本字段和一个按钮在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=""/>

试试

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

popupWindow.textBoxId = 'test value';

您需要获取输入文本DOM元素'textBoxId'并设置该获得元素的'value'属性。

这行不对

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();
}