根据用户输入更改按钮的URL

时间:2016-06-22 19:45:12

标签: javascript

我找到了这段代码:



<a id="reflectedlink" href="http://www.google.com/search">http://www.google.com/search</a>
<input id="searchterm"/>

<script type="text/javascript">
    var link= document.getElementById('reflectedlink');
    var input= document.getElementById('searchterm');
    input.onchange=input.onkeyup= function() {
        link.search= '?q='+encodeURIComponent(input.value);
        link.firstChild.data= link.href;
    };
</script>
&#13;
&#13;
&#13;

有没有办法为按钮修改它?我想要一个带有URL的按钮,而不是文本链接,该按钮可以根据用户输入动态更改。

1 个答案:

答案 0 :(得分:-1)

谢谢大家。我发现below code能够满足我的需求。我也会测试上面的内容。

&#13;
&#13;
function submitForm() {
  var name = document.getElementById("name").value;
  if (name == "") {
    alert("Enter the ID#")
  } else {
    window.location = "http://somesite.com/DispForm.aspx?ID=" + name;
  }
}
&#13;
Enter the ID#:
<input id="name" type="text">
<input type="button" onclick="submitForm()" value="Submit">
&#13;
&#13;
&#13;