javascript - 关注表单中的所有元素

标签 javascript jquery html

我有一个包含 10 个输入字段的表单(下面并未包含所有输入字段)。我正在尝试使用 jQuery focusout 函数触发事件。例如,表单名称为:form_test

<form id="form_test">
    <input name="customer_id" id="customer_id" hidden>
    <input name= "customer_name" id= "customer_name" type="text" class="form-control" placeholder="Company Name">
</form>

我希望发生的是,无论他们点击哪个输入框,点击后,焦点事件都会发生。

最佳答案

每次输入获得焦点然后焦点被移除时,focusout事件都会触发。下面的代码片段展示了如何绑定(bind)到该事件。

$(function() {

  // Using the id
  $("#form_test input").on('focusout', function() {
    console.log("focusout event triggered using the id!");
  });

  // Using the name
  $("form[name='form_test'] input").on('focusout', function() {
    console.log("focusout event triggered using the name!");
  });

});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form name="form_test" id="form_test">
  <input name="customer_id" id="customer_id" hidden>
  <input name="customer_name" id="customer_name" type="text" class="form-control" placeholder="Company Name">
</form>

关于javascript - 关注表单中的所有元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54975971/

相关文章:

javascript - Vue JS 组件如何在 Twig x 模板文件中工作

javascript - Uncaught ReferenceError : FB is not defined

jquery - 在 jquery 中处理 $(window).scroll 函数的更有效方法?

javascript - jquery: $.post 成功处理程序不接受 else {}

javascript - 更改段落选择样式

html - 列边框不可见

javascript - 在每个页面上执行 Greasemonkey 脚本,无论页面类型如何(如 foo.com/image.jpg)?

javascript - 使用 ExtJS 应用程序更新 ADAL.js token

javascript - 未知的 node.js 和 websockets 服务器停止。谁修改我的代码?

javascript - 如何使列表项具有均匀大小的高度