javascript Enter按键并输入检查

时间:2018-07-02 08:46:55

标签: javascript model-view-controller

我要添加Enter键以登录系统。 JavaScript块具有检查功能。如何添加Enter键功能?这是查看代码。

@using (Html.BeginForm("Login", "InventoryBarcode", FormMethod.Post, new { id = "main" }))
{
    <label for="LoginName" class="uname">Account</label>
    <input type="text" name="LoginName" id="LoginName" value="" placeholder="Please Enter Account" />
    <label for="LoginPassword" class="youpasswd">Password</label>
    <input type="password" name="LoginPassword" id="LoginPassword" value="" placeholder="Please Enter Password" />
    <asp:TextBox ID="txtLoginPassword" runat="server" TextMode="Password" placeholder="Please Enter Password"></asp:TextBox>
    <span id="message" style="color:red">@ViewBag.Message</span>
    <input type="button" name="Login" id="Login" value="login" onclick="Check();" />
}

<script src="~/Scripts/jquery-3.1.1.js"></script>
<script type="text/javascript">
function Check() {
    if ($.trim($("#LoginName").val()) === "") {
        $("#message").text('Please Enter Account!');
        return false;
    }
    if ($.trim($("#LoginPassword").val()) === "") {
        $("#message").text('Please Enter Password!');
        return false;
    }
    $("#Login").hide();
    $("#message").text('Login Please Wait...');
    $("#main").submit();
}
</script>

1 个答案:

答案 0 :(得分:1)

我强烈建议您附加提交处理程序:

<input type="submit" name="Login" id="Login" value="login" />
$("#main").on("submit", function(e) {
  if ($.trim($("#LoginName").val()) === "") {
    $("#message").text('Please Enter Account!');
    return false;
  }
  if ($.trim($("#LoginPassword").val()) === "") {
    $("#message").text('Please Enter Password!');
    return false;
  }
  $("#Login").hide();
  $("#message").text('Login Please Wait...');
});