干货 Tips Rules & Tips
1. 遵守中国大陆相关法律法规
2. 本版还在调整当中

搜索输入关键字回车无效(点击提交按钮有效)的问题

查看: 2634|回复: 0
虫子 发表于 2015-3-9 11:12:39
本帖最后由 张大虫 于 2015-3-9 11:29 编辑

代码如下:

  1. <div style="background:url(http://64.233.166.99/images/srpr/logo11w.png) no-repeat;background-size:269px 95px;width:269px;height:95px;margin:200px auto 30px;"></div>
  2. <form id="form1" action="http://64.233.166.99/search" target="_self">
  3.       <input type="hidden" name="hl" value="zh-CN" />
  4.       <div style="width:500px;margin:0 auto;padding:2px"><input id="q" type="text" name="q" autocomplete="off" title="Google 搜索" aria-label="搜索" aria-haspopup="false" role="combobox" aria-autocomplete="both" dir="ltr" spellcheck="false" style="border:none;min-width:500px;height:20px;border:solid 1px #777;padding:3px;" /> </div>
  5.       <input type="button" onclick="doSearch()" value="谷歌谷歌" style="display:block;padding:5px 11px;margin:20px auto auto;border-radius:0 5px 0 5px;color:#777"/>
  6.      </form>
  7. <script type="text/javascript">
  8.         function doSearch() {
  9.                 var q = document.getElementById('q').value;
  10.                 var url = "http://64.233.166.99/search?hl=zh-CN&q=" + q;
  11.                 top.location.href = url;
  12.         }
  13. </script>
复制代码

也就是快搜页的第二个谷歌搜索。。。之后发现将第5行的type="button" 改为type=“submit”就可以了——why?

查阅为ITEYE资料显示:

type="button" ,"submit" 的区别

  Submit是专门用于提交表单的Button,与Button的区别主要有两点:
  type=button 就单纯是按钮功能
  type=submit 是发送表单
   (1)Submit将表单提交(form.submit())作为其onclick后的默认事件,Button并非如此
   (2)表单提交时,所有具有name属性的html输入元素(包括input标签、button标签、select标签等)都将作为键值对提交,除了Submit对象。Submit对象只有在自己被单击后的提交中才会作为键值对被提交。

  但是对于从事WEB UI的人应该要注意到,使用submit来提高页面易用性:
  
  使用submit后,页面支持键盘Enter键操作,而很多WEB软件设计师,可能没有注意到submit统一
  
  用button后往往页面不支持Enter键了。所以需要支持Enter键,必须要设置个submit,默认enter键对页面第一个submit进行操作
  
  
  执行完onClick,转到action。可以自动提交不需要onClick。所以说onclick这里可以不要。
  
  执行完onClick,跳转文件在 js文件里控制。提交需要onClick,比如:

  1,onclick="form1.action='a.jsp';form1.submit();" 这样就实现了submit的功能了。
  
     说通俗一些,就是submit会有一个跳转,页面会刷新;而button不会刷新,就是一个button;可以用<button type="submit/button/reset"></button>来生成按钮,更加灵活,样式更好控制。  

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

虫部落 陕ICP备14001577号-1川公网安备 51019002003015号联系我们FAQ关于虫部落免责声明虫部落生存法则蛙先知 - AI 玩家社区 🚧

Build with for "make search easier" Copyright © 2013-2024. Powered by Discuz! GMT+8, 2024-5-9 17:49

快速回复 返回顶部 返回列表