简单的JavaScript搜索框 [英] Simple JavaScript search box

查看:69
本文介绍了简单的JavaScript搜索框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试连接第一个文本框,以便将其转换为URL,然后单击搜索"时,它将跳转到该网站,不确定是否不可能,或者我很无知,非常感谢您的帮助,在此先感谢您!

I tried connecting the first text box, so it would turn it into a URL, and then when 'search' is clicked, it would jump to that website, not sure if it's impossible, or i'm just clueless, but would really appreciate some help, thank you in advance!

  <html>
    <head>
    </head>
  <body>
    <script type="text/javascript">

    function link() {
        var link_s = document.getElementById('link_id').value;
        document.getElementById('link_str').innerHTML = link_s.link()
    }

    </script>

<h2> Search box </h2>
    <input type='text' id='link_id'>
        <input type='button' id='link_str' value='Search' onClick='link()'>
    </body>
  <html>

推荐答案

尝试以下JavaScript:

Try this JavaScript:

function goTo()
{
    location.href = document.getElementById('link_id').value;
}

并更改HTML中的onclick:

<input type='text' id='link_id'>
<input type='button' id='link' value='Search' onClick='javascript:goTo()'>

如果要遵循不干扰JavaScript的方式,则可以将onclick完全移到JavaScript代码中,然后将其从HTML中删除:

If you want to follow the unobtrusive JavaScript way, you would move the onclick completely into your JavaScript code, and remove it in the HTML:

document.getElementById('link').onclick = function()
{
    location.href = document.getElementById('link_id').value;
};

这篇关于简单的JavaScript搜索框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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