【问题标题】:Make cursor leave text field with jQuery使用 jQuery 使光标离开文本字段
【发布时间】:2013-03-21 21:13:12
【问题描述】:

我有一个输入 type=text 的 html 表单,它在提交时启动一个自定义 js 函数。当用户点击 ENTER 时,一个 jQuery 监听器调用相同的函数。

这意味着:用户可以在按下提交按钮和按 ENTER 之间进行选择。

由于我的 js 函数让用户停留在当前页面上,因此光标在上次聚焦的文本字段中保持活动状态。所以用户可以一直按 ENTER 并启动 js 函数。当然,我可以尝试使用诸如“didEnterFunction”之类的布尔值来防止这种情况发生。但是我可以强制光标离开文本字段吗?那会更优雅。

我想像这样:

<form method="post" onsubmit="return myJS();">
    <input type="text" name="request" />
    <input type="submit" value="OK" />
</form>

function myJS() {
    // Make the cursor leave the text field "request"
    ?????

    return no; // prevent submission of the form (reload)
}

【问题讨论】:

标签: jquery text input cursor focus


【解决方案1】:

试试这个:

function myJS() {
    // Make the cursor leave the text field "request"
    $('input[type="submit"]').focus();

    return false; // prevent submission of the form (reload)
}

【讨论】:

  • 我认为这个想法是正确的。但是,当我按 Enter 键时,将焦点移到提交按钮会再次提交表单!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-04-23
  • 1970-01-01
  • 2020-10-31
  • 1970-01-01
  • 2011-11-24
  • 1970-01-01
相关资源
最近更新 更多