Javascript 简单表单提交代码
我想创建一个只有输入文本框和搜索按钮的简单表单 人们会在框中键入搜索词,然后按search 所要做的就是将他们的搜索词添加到url的末尾 因此,如果搜索词是“好的意大利食品”,提交按钮会将用户发送到意大利食品 最简单的方法是什么Javascript 简单表单提交代码,javascript,html,forms,Javascript,Html,Forms,我想创建一个只有输入文本框和搜索按钮的简单表单 人们会在框中键入搜索词,然后按search 所要做的就是将他们的搜索词添加到url的末尾 因此,如果搜索词是“好的意大利食品”,提交按钮会将用户发送到意大利食品 最简单的方法是什么 谢谢大家! 使用javascript和html表单 使用按钮和文本框创建表单 在表单的post中,调用javascript函数创建带有用户输入文本的url <!DOCTYPE html> <html> <head> <scrip
谢谢大家! 使用javascript和html表单 使用按钮和文本框创建表单 在表单的post中,调用javascript函数创建带有用户输入文本的url
<!DOCTYPE html>
<html>
<head>
<script type='text/javascript'>
function redirect(){
var s = document.getElementbyID("txt").value;
var url="http://url.com/" + s;
window.location = url;
}
</script>
</head>
<body>
<form>
<input type=''></input>
</form>
</body>
</html>
函数重定向(){
var s=document.getElementbyID(“txt”).value;
变量url=”http://url.com/“+s;
window.location=url;
}
函数搜索(){
var关键字=document.getElementById(“关键字”).value;
变量url=”http://yourwebsite.com/find/“+关键字;
window.location=url;
返回false;
}
我还建议使用encodeURI()使其“安全”
您可以覆盖onsubmit事件中的action属性: HTML 此外,在
onsubmit
事件中重写表单操作或重定向用户将允许您使用truesubmit
按钮
这使用户能够在完成键入提交搜索后点击
enter键
,而无需编写额外的逻辑来监听文本框中的enter键以提交搜索。谢谢。总有一个?在url的末尾。我该怎么去掉它?哇,我没注意到。如果将form方法设置为method=“post”
,则应删除?
<html>
<head>
<script type='text/javascript'>
function Search() {
var keyword = document.getElementById("keyword").value;
var url = "http://yourwebsite.com/find/"+keyword;
window.location = url;
return false;
}
</script>
</head>
<body>
<form name="search">
<input type="text" name="keyword" id="keyword" />
<input type="button" name="btnser" onclick="Search()" value="Search" />
</form>
</body>
</html>
<form action="#" onsubmit="send(this)" method="post" target="_blank" >
<input id="text"/>
<input type="submit" value="submit"/>
</form>
function send( form){
form.action = location.href + '/'+encodeURI(document.getElementById('text').value);
}