0
當「enter」被擊中並執行不同的操作時,我需要兩個輸入不提交,所以我捕獲兩個字段輸入按鍵並返回false,但表單仍然提交。爲什麼?表單上的jquery返回false實際上是提交
@model Venta
@{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Venta</h2>
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@using (Html.BeginForm("Create", "Venta", FormMethod.Post))
{
<div class="form-horizontal col-md-12" id="DivForm">
<div class="form-group col-md-6">
@Html.LabelFor(model => model.Codigo_, htmlAttributes: new { @class = "col-md-5 control-label" })
<div class="col-md-7">
@Html.TextBoxFor(model => model.Codigo_, htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.Codigo_)
</div>
</div>
<div class="form-group col-md-6">
@Html.LabelFor(model => model.Nombre_, htmlAttributes: new { @class = "col-md-5 control-label" })
<div class="col-md-7">
@Html.TextBoxFor(model => model.Nombre_, htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.Nombre_)
</div>
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Agregar Stock" class="btn btn-default btn-primary btn-block" />
</div>
</div>
//More filds here
}
<script>
$(function() {
$("#DivForm #Codigo_, #DivForm #Nombre_").bind('keyup', function (e) {
if (e.keyCode === 13) { // 13 is for enter key
alert(); // Do something else
return false;
}
});
});
</script>
當我在第一次兩個領域,這是兩個我說的是我的,我得到的警報()和也提交表單。 「返回false」不起作用。
查看event.preventDefault()方法。可以幫助 –