js怎么实现:输入框输入,按button后跳转到第二页得到参数,给出搜索结果

power333 2016-06-27 04:35:08

如本论坛一样,在首页输入查询关键字,在另一页得到参数并显示出来,我只需要js代码,比如form,input,button
...全文
511 8 打赏 收藏 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
power333 2016-06-28
  • 打赏
  • 举报
回复
引用 7 楼 power333 的回复:
[quote=引用 2 楼 perhapschen 的回复:] 两个页面,利用URL传参 1. search页面

<meta charset="utf-8">
<h1>search页</h1>
<input type="text" id="txt"><input type="button" id="btn" value="搜索">
<script type="text/javascript">
window.onload = function(){
  var txt = document.getElementById("txt"),
      btn = document.getElementById("btn");
  btn.onclick = function(){
    window.location.href = "result.html?keywords=" + txt.value;
  }
}
</script>
2. result页面

<meta charset="utf-8">
<h1>result页</h1>
<input type="text" id="txt"><input type="button" id="btn" value="搜索">
<script type="text/javascript">
window.onload = function(){
  var keyWords = window.location.search.split("=")[1],
      txt = document.getElementById("txt");
  txt.value = decodeURI(keyWords);
}
</script>
参数已经传递到第二页的输入框里了,下一步是怎么让第二页自动使用该参数执行搜索呢,我的搜索函数代码已经写好了,现在它不自动执行,我需要再第二页手动点button才行。[/quote]谢谢,已经成功了。
power333 2016-06-28
  • 打赏
  • 举报
回复
引用 2 楼 perhapschen 的回复:
两个页面,利用URL传参 1. search页面

<meta charset="utf-8">
<h1>search页</h1>
<input type="text" id="txt"><input type="button" id="btn" value="搜索">
<script type="text/javascript">
window.onload = function(){
  var txt = document.getElementById("txt"),
      btn = document.getElementById("btn");
  btn.onclick = function(){
    window.location.href = "result.html?keywords=" + txt.value;
  }
}
</script>
2. result页面

<meta charset="utf-8">
<h1>result页</h1>
<input type="text" id="txt"><input type="button" id="btn" value="搜索">
<script type="text/javascript">
window.onload = function(){
  var keyWords = window.location.search.split("=")[1],
      txt = document.getElementById("txt");
  txt.value = decodeURI(keyWords);
}
</script>
参数已经传递到第二页的输入框里了,下一步是怎么让第二页自动使用该参数执行搜索呢,我的搜索函数代码已经写好了,现在它不自动执行,我需要再第二页手动点button才行。
  • 打赏
  • 举报
回复
查询需要服务器端来做,不是客户端js做的,操作不了服务器端数据库
qq_33468192 2016-06-27
  • 打赏
  • 举报
回复
用缓存啊。。。
止戈谓之武 2016-06-27
  • 打赏
  • 举报
回复
这个不是JS的工作吧,HTML这块FORM表单POST提交到PHP,然后PHP处理页面GET下就行了
power333 2016-06-27
  • 打赏
  • 举报
回复
你好,我的第一页和第二页都是普通的html,不是aspx类型的,第二页也是一个带有输入框的页面。 我是想在第一页的输入框输入查询,按button,然后调整到第二页,第二页也类似第一个,只是我把搜索结果呈现在第二页去。
cocotsau 2016-06-27
  • 打赏
  • 举报
回复
两个页面,利用URL传参 1. search页面

<meta charset="utf-8">
<h1>search页</h1>
<input type="text" id="txt"><input type="button" id="btn" value="搜索">
<script type="text/javascript">
window.onload = function(){
  var txt = document.getElementById("txt"),
      btn = document.getElementById("btn");
  btn.onclick = function(){
    window.location.href = "result.html?keywords=" + txt.value;
  }
}
</script>
2. result页面

<meta charset="utf-8">
<h1>result页</h1>
<input type="text" id="txt"><input type="button" id="btn" value="搜索">
<script type="text/javascript">
window.onload = function(){
  var keyWords = window.location.search.split("=")[1],
      txt = document.getElementById("txt");
  txt.value = decodeURI(keyWords);
}
</script>
persuit666 2016-06-27
  • 打赏
  • 举报
回复
<form name='form1' method='post' > <input type='text' id='txtKey' name='txtKey'><input type='button' onclick='query()' value='搜一搜'> </form> <script> function query() { var key=document.getElementById('txtKey').value; if(!key){ alert('请输入关键字!');return ;} form1.action='xxx.aspx'//这里是跳转页面的地址,你可以在它后台接收传递的参数 Request["txtKey"]这个就是关键字 form1.submit() } </script>

87,984

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧