使用Shift + Tab返回时自动对焦

时间:2018-08-06 09:30:42

标签: javascript jquery javascript-events keypress

这是我的表格,如果我在“密码”字段上并想使用 Shift + Tab 返回EMAIL字段。我该如何使其聚焦,我是说不需要单击“电子邮件”字段进行编辑?

这是我的表单逻辑

  1. 所有字段的初始条件均为只读/禁用,除了第一个字段,用户无法转到下一个字段并单击下一个字段

2。仅关注正在填充的字段,其他字段将被禁用/只读。

3。如果我填写电子邮件,密码和数字字段将被禁用,

4。移至密码字段时,电子邮件和数字被禁用,您可以返回电子邮件字段,但如果密码字段仍为空,则无法转到数字字段

<!DOCTYPE html>
<html lang="en">
<head>
  <title>Bootstrap Example</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>

<div class="container">
  <h2>Vertical (basic) form</h2>
  <form action="/action_page.php" method="post" name="subForm" id="subForm">
    <div class="form-group">
      <label for="email">Email:</label>
      <input type="email" class="form-control" id="email" placeholder="Enter email" name="email">
    </div>
    <div class="form-group">
      <label for="pwd">Password:</label>
      <input type="password" class="form-control" id="password" placeholder="Enter password" name="password">
    </div>
     <div class="form-group">
      <label for="pwd">Number:</label>
      <input type="number" class="form-control" id="number" placeholder="Enter Number" name="number">
    </div>

    <button type="submit" class="btn btn-default">Submit</button>
  </form>
</div>
<script type="text/javascript">
function disableinputs(){
    $("#subForm input").prop('readonly', true);
}
$(document).ready(function(){
    disableinputs();
});
$(document).on('click','#subForm input',function(){
    $(this).prop('readonly', false);
});
$(document).on('focus','#subForm input',function(){
    $(this).prop('readonly', false);
});
$(document).on('focusout','#subForm input',function(){
    disableinputs();
});

</script>
<script type="text/javascript">
    $("#email").focusout(function () {
        if (!$(this).val()) {
            $(this).focus();
        }
    });
    $("#password").focusout(function () {
        if (!$(this).val()) {
            $(this).focus();
            $("#email").focus();
        }
    });
    $("#number").focusout(function () {
        if (!$(this).val()) {
            $(this).focus();
            $("#number").focus();
        }
    });
    </script>
</body>
</html>

1 个答案:

答案 0 :(得分:0)

感谢您的回复。

这是我的表单逻辑

  1. 所有字段的初始条件都是只读/已禁用,除了第一个字段

  2. 仅关注正在填充的字段,其他字段将被禁用/只读。

  3. 如果我填写电子邮件,密码和数字字段将被禁用,

  4. 当移动到密码字段时,电子邮件和数字被禁用,您可以返回到电子邮件字段,但是如果密码字段仍然为空,则不能转到数字字段

相关问题