首页 > 解决方案 > 你能做一个可变的href,而不是一个按钮,只有一个文本框

问题描述

我正在制作一个搜索引擎,我想知道您是否可以制作一个可更改的href,而不是button只有一个textbox. 这就是我所拥有的:所以我可以使用一种不同的方式让我能够按 Enter 键而不是让我知道

<input id="input" type="url"></input>
<button id="link button"><a id="link" style="text-decoration: none; color: inherit;" href="" TARGET="_BLANK">enter</a></button>
<script src="script.js">
    var linkButton = document.getElementById("link button")
    linkButton.onclick = function(){
        var input = document.getElementById("input").value
        document.getElementById("link").setAttribute("href", "http://"+input)
    }
</script>

标签: javascripthtmlhref

解决方案


另一种选择是使用表格

function myfunction(){
var inputbox = document.getElementById('inputval');
 document.querySelector('#check').innerText = inputbox.value;
 window.open('http://'+inputbox.value);

}
<form onsubmit="myfunction();return false;">
  <input name="value" id="inputval"/>
  <p id="check"></p>
</form>


推荐阅读