以下编码工作正常
<html>
<head>
</head>
<body>
<select name="menu" id="menu">
<option value="http://www.google.com">ESPN</option>
<option value="http://www.facebook.com">CNN</option>
</select>
<script type="text/javascript">
var urlmenu = document.getElementById( 'menu' );
urlmenu.onchange = function() {
window.open( this.options[ this.selectedIndex ].value );
};
</script>
</body>
</html>
我需要在下拉列表旁边添加一个按钮,以便在单击按钮后允许链接工作。
答案 0 :(得分:1)
这就是你想要的吗?
var urlmenu = document.getElementById('menu');
var btn = document.getElementById("btn");
// Set up a click event handling function for the button
btn.addEventListener("click", function() {
// Confirmation of action for testing
console.log("Navigating to:" + urlmenu.value);
// Open new window with correct URL
window.open( urlmenu.value );
});
&#13;
<select name="menu" id="menu">
<option value="http://www.google.com">Google</option>
<option value="http://www.facebook.com">Facebook</option>
</select>
<button id="btn">Go!</button>
&#13;
答案 1 :(得分:0)
<html>
<head>
</head>
<body>
<select name="menu" id="menu">
<option value="http://www.google.com">ESPN</option>
<option value="http://www.facebook.com">CNN</option>
</select>
<input type="button" id="btn" value="CLICK ME" onClick="Clicked();"/>
<script type="text/javascript">
function Clicked() {
var urlmenu = document.getElementById( 'menu' );
window.open( urlmenu.options[ urlmenu.selectedIndex ].value );
};
</script>
</body>
</html>
抱歉,如果我更改了你的代码......