2015-12-31 519 views
3

我有select列表,當我點擊go按鈕時轉到外部鏈接。但鏈接在同一個窗口中打開。我想讓它打開新窗口。這是我的代碼。window.location在新標籤頁中打開

<select id="my-dropdown" name="my-dropdown"> 
    <option value="http://www.google.com">A</option> 
    <option value="http://www.aol.com">B</option> 
</select> 
<button class="go-btn" type="submit">Go</button> 

的Javascript:

$('.go-btn').click(function() { 
    window.location = $('#my-dropdown').val(); 
});  

任何人都可以幫助嗎?

+0

一些建議的答案會觸發彈出式窗口攔截器。這是你的問題嗎? – colecmc

回答

3

使其

$('.go-btn').click(function() { 
    window.open($('#my-dropdown').val()); 
});  

$('.go-btn').click(function() { 
    window.open($('#my-dropdown').val(), "_newtab"); 
});  
0

我想你可以試試這個太

window.open($('#my-dropdown').val(), '_blank'); 
0

嘗試用window.open()

$('.go-btn').click(function() { 
window.open($('#my-dropdown').val(), '_blank'); 
}); 

Working Demo

0

純JavaScript解決方案:

<select id="my-dropdown" name="my-dropdown"> 
    <option value="www.google.com">A</option> 
    <option value="www.aol.com">B</option> 
</select> 
<button class="go-btn" type="button" onclick="go();">Go</button> 

<script> 
    function go() { 
    var e = document.getElementById("my-dropdown"); 
    var url = e.options[e.selectedIndex].value; 
    window.open(url, "_newtab"); 
    }; 
</script> 
0

請檢查該

<select id="my-dropdown">      
    <option value="http://www.google.com">A</option> 
    <option value="http://www.facebook.com">B</option> 
</select> 
<button class="go-btn" type="submit">Go</button> 

$(document).ready(function(){ 
    $('.go-btn').click(function() { 
     window.open($('#my-dropdown').val(), '_blank'); 
    }); 
}); 
0

這是一個不同的方法,不會觸發瀏覽器彈出窗口阻止程序作爲window.open有時能夠。

動態地創建一個鏈接與您的下拉列表的值以及target="_blank",然後強制點擊它。

看到它在jsbin

var setAttributes = function(d, b) { 
 
    for (var h in b) b.hasOwnProperty(h) && d.setAttribute(h, b[h]); 
 
    return d; 
 
}; 
 

 
var makeLink = function(id, href) { 
 
    var link = document.createElement('a'), 
 
    attr = { 
 
     'id': id, 
 
     'href': href, 
 
     'target': '_blank', 
 
     'style':'display:none' 
 
    }; 
 

 
    link.insertAdjacentHTML('beforeend', href); 
 
    return setAttributes(link, attr); 
 
}; 
 

 
$(document).ready(function() { 
 
    $('.go-btn').click(function() { 
 
    var theLink,myLink = makeLink('the-link', $('#my-dropdown').val()); 
 

 
    document.body.insertAdjacentHTML('beforeend',myLink.outerHTML); 
 
    theLink = document.getElementById('the-link'); 
 
    console.log(theLink); 
 
    theLink.click(); 
 
    theLink.parentElement.removeChild(theLink); 
 
    }); 
 
});
<!DOCTYPE html> 
 
<html> 
 

 
<head> 
 
    <title>Open in new tab - No pop up blocker</title> 
 
    <meta charset="utf-8"> 
 
</head> 
 

 
<body> 
 

 
    <select id="my-dropdown" name="my-dropdown"> 
 
    <option value="http://www.google.com">A</option> 
 
    <option value="http://www.aol.com">B</option> 
 
    <option value="http://stackoverflow.com/">C</option> 
 
    </select> 
 
    <button class="go-btn" type="submit">Go</button> 
 
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
</body> 
 

 
</html>

0

您可以使用下面

$('.go-btn').click(function() { 
    window.open($('#my-dropdown').val(), "_blank"); 
}); 
0

這裏是如何在新標籤中打開它,但我不知道窗口:

$('.go-btn').click(function() { 
    window.open("#my-dropdown", "_blank"); 
}); 
相關問題