我正在製作一個網頁,以便我可以從自定義頁面搜索谷歌。 但是,當我點擊搜索它確實搜索我的輸入谷歌,但是當我輸入#和%和所有這些字符時,URL應該是不同的。 所以我的問題是我怎麼轉換是這樣的:HTML和JavaScript使搜索輸入準備好了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());
}
);
and
$('button#SubmitSearch').click
(
function()
{
window.location = "https://www.google.nl/#q=" + $('input#SearchBar').val();
}
);
您需要對搜索文本進行編碼。有一個方便的Javascript函數可以做到這一點,看看'encodeURIComponent' –