2017-02-12 59 views
-1

我有一個textarea文本「ez.aaaa.value」我想輸入文本,當我選擇選項時,自動重定向到google.com/search?tbm=isch & q = +((value textarea))如何在選項選擇中添加值?

image

例子:

........ 
<option value="href='http://www.google.com/search?tbm=isch&q=' + ez.aaaa.value">google search</option> 
........ 
+0

這是什麼選擇?對於 –

+0

解釋你的問題 –

+0

你試過'$(「#myselect option:selected」).text(); (或val())' –

回答

2

這應該這樣做。

location.href是你會用來鏈接的。

this指的是select和selectedIndex返回所選選項的編號。

var sel = document.getElementById('mysel'), 
 
    query = document.getElementById('myquery'); 
 

 
sel.addEventListener('change' , function() { 
 
    console.log(this.options[this.selectedIndex].value + query.value); 
 
    
 
    // this would be how to link below 
 
    // location.href = this.options[this.selectedIndex].value + query.value; 
 
});
<input type='text' id="myquery"> 
 
<select id="mysel"> 
 
    <option>SELECT ONE</option> 
 
    <option value="http://google.com?q=">google</option> 
 
    <option value="http://bing.com?q=">bing</option> 
 
</select>

+0

在我的代碼中刪除位於location.href前面的//。 – creativekinetix

0

與ID添加selectoption的外面再嘗試這種方式

JS

var e = document.getElementById("select-menu"); 
var strUser = e.options[e.selectedIndex].value; 
console.log(strUser); 

HTML

<select id="select-menu"> 
<option value="href='http://www.google.com/search?tbm=isch&q=' + ez.aaaa.value">google search 1</option> 
<option value="href='http://www.google.com/search?tbm=isch&q=' + ez.aaaa.value" selected="selected">google search 2</option> 
</select> 
+0

謝謝,我怎麼能自動重定向鏈接到「href ='http://www.google.com/search?tbm = isch&q ='+ ez.aaaa.value」 – moon90

+0

@ moon90使用'window.location = strUser;在相同的標籤中重定向。 –

+0

1)使用