2013-04-02 31 views
0

我有代碼,它有一個文本字段和一個用javascript編寫的按鈕。當用戶點擊按鈕時,它會打開一個到網站搜索頁面的新窗口。我想要做的就是使用它的ID填寫搜索字段,然後激活網站的搜索按鈕。我似乎沒有能夠傳遞給外部網站的文本字段的文本,但這是我的。通過編號填寫textfield javascript

<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=""/> 
+0

是否您的網站和彈出式源代碼在同一個域中? – PSL

+0

@PSCoder否,彈出窗口是外部網站 – user818502

+0

嗯。也許,如果它是另一個域名,你不能這麼做。 – apast

回答

1

嘗試像

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

,而不是

popupWindow.textBoxId = 'test value'; 
+0

我已經嘗試了許多變化,這只是似乎沒有任何效果。儘管謝謝你的回答! – user818502

+0

我認爲它不可能然後:) – dimaninc

+0

aww無益。我真的認爲這將是非常整潔!嗯,好吧 – user818502

1

你需要讓輸入文本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(); 
} 
+0

感謝您的解決方案,我試過了,但它似乎並沒有填補領域。它在打開新窗口後似乎停止了JavaScript代碼。我會附上我從網站源代碼獲得的代碼(我只是給你鏈接,但它只能在網絡上本地訪問 – user818502