按 Enter 按钮不提交,只单击按钮

Pressing Enter the button doesn't submit, only clicking the button

所以这是我的登录模式中的一个按钮,只有在我单击该按钮时才提交,但我希望客户端也可以按 Enter 键并提交,但是不起作用!

这是我的<按钮>:

<button id="loginsubmitbutton" type="submit" 
class="btn btn-default">Enter</button>

这是我的<脚本>:

<script>
    console.log('script')

    $('#loginsubmitbutton').on('click', function (e) {
        e.preventDefault();
        console.log('estou aqui')


        let username = $("#username").val();
        let password = $("#password").val();

      let data = {
            username: username,
            password: password
        }
      $.ajax({
          type: 'POST',
          url: '/',
          data: JSON.stringify(data),
          contentType: "application/json",
          success: function (data) {
          window.location.href = '/dashboard';
            },
          error: function () {
           $('#alert').append(
             `<div class="alert alert-danger" role="alert" style="font-size: 11px; color: #c50e0e;">
                    Invalid username or password. Please, try again.</div>`)
            }
        })
    });
</script>

嗯,您正在使用点击功能。您可以改用表单提交事件,或为代码 13 添加 keyup 事件。