Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
HTML和JavaScript使URL的搜索输入就绪_Javascript_Html_Url_Search Engine_Google Search - Fatal编程技术网

HTML和JavaScript使URL的搜索输入就绪

HTML和JavaScript使URL的搜索输入就绪,javascript,html,url,search-engine,google-search,Javascript,Html,Url,Search Engine,Google Search,我正在制作一个网页,这样我就可以从自定义页面搜索谷歌。 但当我点击搜索时,它会在谷歌上搜索我的输入,但当我输入#和%以及所有这些字符时,URL应该是不同的。 所以我的问题是我如何转换这样的东西: http://www.google.com/#q=Hello World C# 为此: http://www.google.com/#q=HEllo+world+C%23 使用JavaScript 编辑: 这是我尝试使用的函数 $('button#SubmitSearch').click

我正在制作一个网页,这样我就可以从自定义页面搜索谷歌。 但当我点击搜索时,它会在谷歌上搜索我的输入,但当我输入#和%以及所有这些字符时,URL应该是不同的。 所以我的问题是我如何转换这样的东西:

http://www.google.com/#q=Hello World C#
为此:

http://www.google.com/#q=HEllo+world+C%23
使用JavaScript

编辑: 这是我尝试使用的函数

    $('button#SubmitSearch').click
    (
        function()
        {
            window.location = encodeURIComponent("https://www.google.nl/#q=" + $('input#SearchBar').val());
        }
    );

使用,例如:

从您的OP中:

$('button#SubmitSearch').click(function(){
    window.location = "https://www.google.nl/#q=" + encodeURIComponent($('input#SearchBar').val());
});

使用函数encodeUri组件:

encodeURIComponent('this is a string with special characters like # and ?')
希望有帮助:)

试试这个 Var search=document.getElementById(“searchbox”); 然后使用addeventlistener或onClick

key=key.replace(//g,“+”);
文档位置(“+search.value”)

您需要对搜索文本进行编码。有一个方便的Javascript函数可以做到这一点,看看
encodeURIComponent
,我甚至不会提到您的或解决方案,因为在这种情况下,它有点低劣,因为您需要附加基本URL。
console.log(encodeURIComponent('Hello World C#'));
> "Hello%20World%20C%23"
$('button#SubmitSearch').click(function(){
    window.location = "https://www.google.nl/#q=" + encodeURIComponent($('input#SearchBar').val());
});
encodeURIComponent('this is a string with special characters like # and ?')