gpt4 book ai didi

javascript - 为什么我不去那个 URL?

转载 作者:行者123 更新时间:2023-11-30 09:59:35 24 4
gpt4 key购买 nike

我正在制作一个类似于 Google 的搜索引擎,而且我才刚刚开始制作它。但是有一个问题!当我输入“google translate”时,它不会转到 https://translate.google.com当我用 JavaScript 为它编写代码时: 'window.location.href=' http://translate.google.com ';'.

现在看我的代码,这是我的 search.js:

function search_Results (form) {

var search_term = form.search_box.value; //This is the search term

//About out company
if(search_term == "About" ||
search_term == "about")
{
alert("About");
}

//Google translate
else if(search_term == "Google translate" ||
search_term == "google translate"// ||
//search_term == "Google Translate" ||
// search_term == "google Translate"
)
{
location.href='http://www.example.com';
}
else
{
alert("Extremely sorry! Your search term: '"+search_term+"' does not matches any search documents.");
}
}

这是我的 searchengine.html:

<!DOCTYPE html>
<html>
<head>
<!----The search script---->
<script src="Search/search.js"></script>
<!----The style scripts---->
<link rel="stylesheet" type="text/css" href="GUI/button.css">
<link rel="stylesheet" type="text/css" href="GUI/heading.css">
<link rel="stylesheet" type="text/css" href="GUI/txtinput.css">
</head>
<body>
<center>
<!----Heading---->
<h1 class="heading">Search Engine</h1>
<!----End that---->
<!----Search options---->
<a href="searchImages.html">
<button class="btn">Images</button>
</a>
<a href="searchVideos.html">
<button class="btn">Videos</button>
</a>
<!----End of the search options---->
<!----The main search form---->
<form name="wow" method="">
<br><br><br>
<input type="text" name="search_box" value="Enter Your Search Term" class="css-input">
<br><br>
<input type="submit" value="Search" onClick="search_Results(this.form)" class="btn">
</form>
<div id="div1">
</div>
<!----End of the search form---->
</center>
</body>
</html>

请问有人能帮帮我吗?我也试过这些:

window.location.href="...";
window.location="...";
location.href="...";
location="...";

但这些都没有帮助我。

最佳答案

尝试使用 type="button" 而不是 type="submit"。它会起作用,因为它不会提交表单,而这是提交所做的。

<input type="button" value="Search" onClick="search_Results(this.form)" class="btn">

关于javascript - 为什么我不去那个 URL?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32380244/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com