简体   繁体   English

通过id javascript填写文本字段

[英]filling in textfield by id javascript

I have code that has a text field and a button made in javascript. 我的代码有一个文本字段和一个用javascript制作的按钮。 When the user clicks the button, it opens up a new window to a website's search page. 当用户单击该按钮时,它会打开一个到网站搜索页面的新窗口。 What I wanted to do was to fill in the search field by using it's id and then activate the website's search button. 我想要做的是使用它的id填写搜索字段,然后激活网站的搜索按钮。 I haven't seem to be able to get the text passed to the external site's textfield but here's what I have. 我似乎无法将文本传递到外部网站的文本字段,但这就是我所拥有的。

<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 would be the id of the search textbox on the newly opened window. textBoxId将是新打开的窗口上搜索文本框的id。

If anything doesn't make sense, let me know. 如果有什么不合理的,请告诉我。

here is the source code for the textbox of the external site: 这是外部网站文本框的源代码:

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

try something like 尝试类似的东西

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

instead of 代替

popupWindow.textBoxId = 'test value';

You need to get the input text DOM element 'textBoxId' and set 'value' property of this obtained element. 您需要获取输入文本DOM元素'textBoxId'并设置此获取元素的'value'属性。

This line is wrong 这条线错了

popupWindow.textBoxId = 'test value';

Use following lines to replace it: 使用以下行替换它:

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

Here, I rewrote full function definition to evaluate proposed solution. 在这里,我重写了完整的函数定义来评估提出的解决方案。 Try it and let us know how it worked! 尝试一下,让我们知道它是如何工作的! []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();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM