【问题标题】:Enter Key function is not working correctly in MVC5?Enter Key 功能在 MVC5 中无法正常工作?
【发布时间】:2016-08-01 07:23:48
【问题描述】:

您好,我想通过在 MVC5 中按 enter 键从一个字段导航到另一个字段。所以我使用了 Enter Key 功能代码。代码在下面提到。

我的观点

 <form>
 <fieldset>
 <legend></legend>
 <div class="col-sm-3">
 <div class="form-group">
 <span style="color: #f00">*</span>
 @Html.Label("Customer Name", new { @class = "control-label" })
 @Html.ValidationMessageFor(model => model.CustomerName)
 @Html.TextBoxFor(model => model.CustomerName, new { @class = "form-control required", type = "text" })
     </div>
     </div>
 <div class="col-sm-3">
 <div class="form-group">
 @Html.LabelFor(model => model.Alias, new { @class = "control-label" })
 @Html.DropDownList("SalutationID", null, "Select", new { @class = "form-control" })
 @Html.ValidationMessageFor(model => model.Alias)
     </div>
     </div>
 <div class="col-sm-3">
 <div class="form-group">
 <span style="color: #f00">*</span>
 @Html.Label("Customer Type", new { @class = "control-label" })
 @Html.DropDownList("CustomerTypeID", null, "Select", new { @class = "form-control required" })
    </div>
    </div>
 <hr style="width:100% ; align-self:baseline">
 <h3 style="color: #0000FF">Address</h3>
 <div class="col-sm-3">
 <div class="form-group">
 <span style="color: #f00">*</span>
 @Html.Label("Address Type", new { @class = "control-label" })
 @Html.DropDownList("AddressTypeID", null, "Select", new { @class = "form-control required" })
   </div>
   </div>
<div class="col-sm-3">
<div class="form-group">
<span style="color: #f00">*</span>
@Html.LabelFor(model => model.Street, new { @class = "control-label" })
@Html.TextBoxFor(model => model.Street, new { @class = "form-control", type = "text required" })
@Html.ValidationMessageFor(model => model.Street)
    </div>
    </div>
 <div class="col-sm-3">
 <div class="form-group">
 @Html.LabelFor(model => model.Location, new { @class = "control-label" })
 @Html.TextBoxFor(model => model.Location, new { @class = "form-control", type = "text " })
 @Html.ValidationMessageFor(model => model.Location)
   </div>
   </div>
<div class="col-sm-3">
<div class="form-group">
@Html.LabelFor(model => model.Place, new { @class = "control-label" })
@Html.TextBoxFor(model => model.Place, new { @class = "form-control", type = "text" })
@Html.ValidationMessageFor(model => model.Place)
    </div>
    </div>
</fieldset>
</form>

我的 Jquery 代码

 <script src="http://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
 <script type="text/javascript" src="./javascript.js"></script>
 <script
src="http://maps.googleapis.com/maps/api/js?key=AIzaSyCJnj2nWoM86eU8Bq2G4lSNz3udIkZT4YY&sensor=false">
 </script>
 <script type="text/javascript">
 $("form:not(.filter) :visible:enabled:first").focus();
 $('form > div').keypress(function(e) {
   debugger;
   if (e.keyCode == 13) {
   e.preventDefault();
   if ($(this).next().length > 0) {
   $(this).next().children('div').children(":not(label)").focus();
   } else {
   $("form:not(.filter) :visible:enabled:first").focus();
    }
   }
 });

我试过这段代码。在此代码中,它显示插件错误,靠近下面的插件。

<script type="text/javascript" src="./javascript.js"></script>

我尝试了很多方法来消除这个插件错误,但没有用。请任何人帮我解决这个问题。

提前谢谢..

【问题讨论】:

  • 为什么要在 tab 已经完成这项工作时使用 enter?只需正确设置标签索引即可。为什么要为已经存在的东西创建解决方案?
  • 这是来自 TL 的订单。这是一项任务
  • 我很乐意告诉他/她他/她在浪费你的时间......
  • 我说但是他们在另一个项目中使用了这个功能,所以他们要求我在我的模块中这样做。

标签: c# jquery asp.net-mvc-5


【解决方案1】:

看起来 jQuery 没有初始化。像这样使用它:

 $(function () {

$("form:not(.filter) :visible:enabled:first").focus();
 $('form > div').keypress(function(e) {
   debugger;
   if (e.keyCode == 13) {
   e.preventDefault();
   if ($(this).next().length > 0) {
   $(this).next().children('div').children(":not(label)").focus();
   } else {
   $("form:not(.filter) :visible:enabled:first").focus();
    }
   }
 });

});

请注意,您的整个 javascript 代码应封装在

中
$(function () 
{ 
//Your javascript goes here 
});

我看到的另一个问题是您的本地 javascript 文件“javascript.js”路径不正确。您必须更正它以加载您的 javascript 文件并消除 404 错误。 img.jpg 文件也是如此。

【讨论】:

  • 好的,我有支票 Abey
猜你喜欢
  • 2017-09-23
  • 1970-01-01
  • 2013-11-06
  • 2013-10-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多