Html 简单的 JavaScript 搜索框
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18167236/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Simple JavaScript search box
提问by Samir
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!
我尝试连接第一个文本框,所以它会将它变成一个 URL,然后当点击“搜索”时,它会跳转到那个网站,不确定是否不可能,或者我只是一无所知,但真的很感激一些帮助,在此先感谢您!
<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>
采纳答案by Alex W
Try this JavaScript:
试试这个 JavaScript:
function goTo()
{
location.href = document.getElementById('link_id').value;
}
and change the onclick
in the HTML:
并更改onclick
HTML 中的 :
<input type='text' id='link_id'>
<input type='button' id='link' value='Search' onClick='javascript:goTo()'>
Edit:
编辑:
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:
如果您想遵循不显眼的 JavaScript 方式,您可以将onclick
完全移动到您的 JavaScript 代码中,并在 HTML 中将其删除:
document.getElementById('link').onclick = function()
{
location.href = document.getElementById('link_id').value;
};
回答by vunoo
if you want to create a form that will search google use this:
如果你想创建一个可以搜索谷歌的表单,请使用:
<script type="text/javascript">
function dos12(g1) {
window.open('https://www.google.com/#q='+g1 +" site:linkedin.com", 'G1window');
}
</script>
<form onsubmit="dos12(this.g1.value); return false;">
<input type="text" name="g1" size="20" placeholder="Name" />
<input type="submit" value="Submit" />
Search Linkedin via Google<br />
<br />
</form>
If you want to search a website then you will need to get the search string used. for example, if you want to search the ABN lookup site for Australia you would use the following code.
如果要搜索网站,则需要获取使用的搜索字符串。例如,如果您想搜索澳大利亚的 ABN 查找站点,您可以使用以下代码。
<script type="text/javascript">
function dos10(a1) {
window.open('http://abr.business.gov.au/SearchByName.aspx?SearchText=' + a1, 'a1window');
}
</script>
<form onsubmit="dos10(this.a1.value); return false;">
<input type="text" name="a1" size="20" placeholder="Name" />
<input type="submit" value="Submit" />
ABN Lookup name<br />
<br />
</form>
hope this helps. you don't need to add anything else. Just copy and paste this into notepad or your code editor and save as test.html then open with browser to test it.
希望这可以帮助。你不需要添加任何其他东西。只需将其复制并粘贴到记事本或您的代码编辑器中,然后另存为 test.html,然后用浏览器打开即可进行测试。
回答by Sheng Slogar
Here's how I would do it:
这是我将如何做到的:
<input type="text" id="link-box"/>
<input type="button" id="search-button" value="Search"
onclick="window.location = document.getElementById('link-box').value;"/>
Of course you could do this:
当然你可以这样做:
<script type="text/javascript">
function func(){
window.location = document.getElementById('link-box').value;
}
</script>
onclick="func();"
Or
document.getElementById("search-button").onclick = function(){
window.location = document.getElementById('link-box').value;
};
或者 document.getElementById("search-button").onclick = function(){ window.location = document.getElementById('link-box').value;
};
Or last of all
或者最后
<script type="text/javascript">
document.getElementById("search-button").addEventListener("click", function(){
window.location = document.getElementById('link-box').value;
});
</script>