【问题标题】:Enter not working in code (only submit button)输入在代码中不起作用(仅提交按钮)
【发布时间】:2015-11-08 13:56:30
【问题描述】:

我有以下代码来搜索邮政编码或城市:

  <input type="text" id="zcc" style="background: #FFF;
  padding: 1px 1px 1px 33px;
  width: 206px;
  height: 28px;
  -webkit-border-radius: 20px 20px 20px 20px;
  -moz-border-radius: 20px 20px 20px 20px;
  -khtml-border-radius: 20px 20px 20px 20px;
  border-radius: 20px 20px 20px 20px;" value="" placeholder="Postcode, Stad" />
    <button id="zccfind" style="cursor: pointer;
   color: #FFFFFF;
   line-height: 14px;
   font-family: Arial, Helvetica, sans-serif;
   font-size: 16px;
    height: 26px;
    background-color:#57ACD4;
    border: none;
    //  background: url('../image/button.png') repeat-x;-webkit-border-radius:       20px 20px 20px 20px;">Zoek</button>

现在我们需要点击按钮来激活搜索。 按下回车键时如何让它工作?

谢谢!

【问题讨论】:

  • 把你的完整代码放在这里...
  • 为什么要使用inline 样式,这让它更难理解。只需使用外部样式表。另请关注JavaScriptkey 事件。
  • 设置提交的按钮类型。 type="submit"
  • html 中的 BTW 样式不是一个好习惯
  • 你到底为什么要在一个按钮中注入所有的CSS?!?!创建一个 CSS 类/id 并将其分配给按钮。另外,这里和 PHP 有什么关系?

标签: php button input types submit


【解决方案1】:

我假设您想在#zcc 文本输入上捕获“输入”键调用,您可以使用 jquery 来执行此操作:

$(document).ready(function() {
 $('#zcc').on('keyup', function(e) { //when user presses a button...
   if(e.keyCode == 13) { //if the pressed key is "enter" key
       alert('you submitted form');
     //$("#zccfind").click(); //if you want to submit the button on enter
       return false;
   }
 });
});

【讨论】:

  • $('#zccfind').click();?为什么不直接使用可用的submit() 函数?
  • 是的,我想过这个,但它不是一个表单,它是一个按钮,这就是我写 click() 的原因,我不知道提交是否有效
  • 他希望 form 在输入时提交,因此您将使用 submit(),因为这就是您提交表单的方式。点击按钮是一种浪费。
  • 哦,好吧,对不起,我没想到:) 编辑:但是表格名称是什么?他没有提供足够的细节
  • 没问题,只要明确 OP 想要什么。
猜你喜欢
  • 2014-01-20
  • 1970-01-01
  • 2017-07-28
  • 2012-03-03
  • 2013-04-09
  • 1970-01-01
相关资源
最近更新 更多