2015-09-06 79 views
0

我想將輸入到輸入框中的文本傳遞到URL的中間,該URL將根據用戶「搜索」呈現嵌入式iframe。單詞之間的空格必須自動轉換爲「%20」或「+」。將輸入文本傳遞到URL以呈現新的iframe頁

在這裏看到的想什麼,我實現一個例子:

http://jsfiddle.net/pw3fL453/

Enter a keyword or phrase into the box below to see if it's trending. 
<br> 
<input placeholder="***input***"></input> 

<iframe src="http://www.google.com/trends/fetchComponent?hl=en-US&q=***INPUT***&cid=TIMESERIES_GRAPH_0&export=5&w=500&h=300"></iframe> 
+0

你是什麼實際問題?你試過什麼了?你正在談論你想要的,無處我看到這是我的問題,這是導致問題。有關如何提問的解釋,請參見[如何提問](http://stackoverflow.com/help/how-to-ask)。 – DragonSamu

回答

0

var timeout; 
 
var input = document.querySelector('input'); 
 
var iframe = document.querySelector('iframe'); 
 

 
input.addEventListener('input', function(e){ 
 
    if(timeout !== undefined) 
 
     clearTimeout(timeout); 
 
    
 
    timeout = setTimeout(function(){ 
 
     iframe.src = 'http://www.google.com/trends/fetchComponent?hl=en-US&q='+ input.value +'&cid=TIMESERIES_GRAPH_0&export=5&w=500&h=300'; 
 
     }, 1000); 
 
});
iframe{ 
 
    width:100%; 
 
    height:400px; 
 
} 
 

 
input{ 
 
    margin:40px 0; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<input placeholder="***input***"></input> 
 

 
<iframe src="http://www.google.com/trends/fetchComponent?hl=en-US&q=***INPUT***&cid=TIMESERIES_GRAPH_0&export=5&w=500&h=300"></iframe>

它可能會更好,只是有一個按鈕,雖然,每次您在輸入框上進行更改時,輸入事件都會觸發。

var input = document.querySelector('input'); 
 
var iframe = document.querySelector('iframe'); 
 
var button = document.querySelector('button'); 
 

 
button.addEventListener('click', function(e){ 
 
     iframe.src = 'http://www.google.com/trends/fetchComponent?hl=en-US&q='+ input.value +'&cid=TIMESERIES_GRAPH_0&export=5&w=500&h=300'; 
 
});
iframe{ 
 
    width:100%; 
 
    height:400px; 
 
} 
 

 
input{ 
 
    margin:40px 0; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<input placeholder="***input***"></input><button>go</button> 
 

 
<iframe src="http://www.google.com/trends/fetchComponent?hl=en-US&q=***INPUT***&cid=TIMESERIES_GRAPH_0&export=5&w=500&h=300"></iframe>

+0

FWIW,這個例子中沒有任何東西沒有jQuery沒有辦法很容易做到。 –

+0

是的,我意識到這一點。我從一開始就想到'jQuery'。我會讓它變成香草JS。 – MinusFour

+0

有沒有什麼方法可以寫出來,以顯示按鈕的工作方式?我認爲你是正確的,從UX的角度來看這會更好。 –