访问按钮上的选定链接

时间:2014-03-30 07:04:43

标签: javascript html

如何在新窗口中从外部按钮的下拉列表中访问所选链接?

<select id="atheist">
    <option value="http://google.com"></option>
    <option value="http://yahoo.com"></option
</select>

var select = document.getElementById("select");
select.addEventListener("change",function() { 
    var win = window.open(this.value, "_blank");
    win.focus();
},false);

因此,我只需点击一下按钮就可以从下拉列表中访问/转到所选链接。你能帮忙吗?

2 个答案:

答案 0 :(得分:0)

jsFiddle Demo

<强> HTML

<select id="atheist">
    <option value="http://google.com">Google</option>
    <option value="http://yahoo.com">Yahoo</option>
</select>

<input id="downloadbtn" type="button" value="download" />

<强> JS

function openLink(){
    var e = document.getElementById("atheist").value;
    console.log(e);
    var win = window.open(e, "_blank");
    win.focus();
}

window.onload=function(){
    var downloadbtn = document.getElementById("downloadbtn");
    downloadbtn.onclick=openLink;
};

addEventListener Method jsFiddle

<强> JS

function openLink(){
    var e = document.getElementById("atheist").value;
    var win = window.open(e, "_blank");
    win.focus();
}

window.onload=function(){
    var downloadbtn = document.getElementById("downloadbtn");
    downloadbtn.addEventListener("click", openLink, false);
};

这类似于以下内容:inputing text in textbox

答案 1 :(得分:-2)

我想在这里你想要选择网站,并希望在点击downlad按钮后打开该网站。

<强> HTML

<select id="select">
    <option value="http://google.com">google</option>
    <option value="http://yahoo.com">yahoo</option>
</select>
<button id="myButton">Download</button>

<强> JS

var button = document.getElementById("myButton");
button.addEventListener("click", function() { 
    var select = document.getElementById('select');
    var win = window.open(select.options[select.selectedIndex].value, "_blank");
    win.focus();

},false);

DEMO