将文本框中输入的文字追加到url并打开url链接 [英] Append text input in the the text box to the url and open the url link

查看:48
本文介绍了将文本框中输入的文字追加到url并打开url链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:

<script type="text/javascript">
function changeText2(){
    var userInput = document.getElementById('userInput').value;
    var lnk = document.getElementById('lnk');
    lnk.href = "www.google.com/search/" + userInput;
    lnk.innerHTML = lnk.href;
}


</script>

Here is a link : <a href="" id=lnk>nothing here yet</a> <br>
<input type='text' id='userInput' value=' ' />
<input type='button' onclick='changeText2()' value='Change Link'/>

因此,这基本上执行以下操作:

So, this basically does the following:

用户在文本框中输入一个词,该词会附加在给定链接 (www.google.com/search/) 的末尾.并且链接显示在页面上方.

User enters a word in the text box, and that word is appended at the end of the link given (www.google.com/search/). And the link is displayed above on the page.

但不是在页面上显示链接,我希望它在单击按钮时打开该页面.我该怎么做?

But instead of displaying the link on the page, I want it to open that page when the button is clicked. How can I do that?

推荐答案

见这个fiddleH2>

尝试 window.location 在点击时重定向到页面.

See this fiddle

Try window.location to redirect to the page on click.

也就是说,在你的代码中尝试

That is, in your code try like

<script type="text/javascript">
function changeText2(){
    var userInput = document.getElementById('userInput').value;
    var lnk = document.getElementById('lnk');
    lnk.href = "www.google.com/search/" + userInput;
    window.location = "www.google.com/search/" + userInput;
}
</script>

这篇关于将文本框中输入的文字追加到url并打开url链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆