加载页面的Javascript不工作
我试图从文本输入中获取文本,将其附加到URL,然后使用Javascript转到该URL。但当我提交时,它似乎只是重新加载页面加载页面的Javascript不工作,javascript,Javascript,我试图从文本输入中获取文本,将其附加到URL,然后使用Javascript转到该URL。但当我提交时,它似乎只是重新加载页面 家 函数loadSearchResult(searchToLoad){ window.location=”https://encrypted.google.com/search?q=“+搜索加载; } 搜寻 您需要在提交函数中使用event.preventDefault() 此外,由于很多原因,内联js(html中的onsubmit)是一种不好的做法。以下是一些:
家
函数loadSearchResult(searchToLoad){
window.location=”https://encrypted.google.com/search?q=“+搜索加载;
}
搜寻
您需要在提交函数中使用event.preventDefault()
此外,由于很多原因,内联js(html中的onsubmit)是一种不好的做法。以下是一些:
最好使用javascript获取对表单的引用并附加事件侦听器
<form id="myForm">
<input type="submit" value="Submit">
</form>
您需要在提交函数中使用
event.preventDefault()
此外,由于很多原因,内联js(html中的onsubmit)是一种不好的做法。以下是一些:
最好使用javascript获取对表单的引用并附加事件侦听器
<form id="myForm">
<input type="submit" value="Submit">
</form>
您需要防止
onsubmit
的默认行为您可以使用
返回false
:
<html>
<head>
<title>Home</title>
<link rel="stylesheet" href="main.css" />
<script type="text/javascript">
function loadSearchResult(searchToLoad) {
window.location = "https://encrypted.google.com/search?q=" + searchToLoad;
return false;
}
</script>
</head>
<body>
<form onsubmit="return loadSearchResult(document.getElementById('googleBox').value)" method="post">
<fieldset>
<legend>Search</legend>
<input type="text" id="googleBox">
</fieldset>
</form>
</body>
</html>
家
函数loadSearchResult(searchToLoad){
window.location=”https://encrypted.google.com/search?q=“+搜索加载;
返回false;
}
搜寻
您需要防止onsubmit的默认行为
您可以使用返回false
:
<html>
<head>
<title>Home</title>
<link rel="stylesheet" href="main.css" />
<script type="text/javascript">
function loadSearchResult(searchToLoad) {
window.location = "https://encrypted.google.com/search?q=" + searchToLoad;
return false;
}
</script>
</head>
<body>
<form onsubmit="return loadSearchResult(document.getElementById('googleBox').value)" method="post">
<fieldset>
<legend>Search</legend>
<input type="text" id="googleBox">
</fieldset>
</form>
</body>
</html>
家
函数loadSearchResult(searchToLoad){
window.location=”https://encrypted.google.com/search?q=“+搜索加载;
返回false;
}
搜寻