【问题标题】:Why do I go into PartialView after peres Enter key in the PartialView that is in Modal?为什么在模态的PartialView中按Enter键后进入PartialView?
【发布时间】:2018-03-17 01:26:34
【问题描述】:

我有一个问题,我不知道如何解决它。 为什么我在Modal 中的PartialView 中按Enter 键后进入PartialView? 我的代码是这样的:

  1. Modal中加载PartialView

<script>
        $(".panel-body").on('click', '.btn-success', function () {
            var modal = document.getElementById('AddModal');
            $('.Addspan').load('/Users/Phases/PhaseAdd/');
            modal.style.display = "block";
            window.onclick = function (event) {
                if (event.target == modal) {
                    modal.style.display = "none";
                }
            }
        });
    </script>
<div id="AddModal" class="modal AddModal">
    <div class="modal-dialog modal-lg">
        <div class="well Addspan">

        </div>

    </div><!-- Add modal-dialog -->
</div>
  1. PartialView 中的Controller

    公共 ActionResult PhaseAdd() { ViewBag.CompanyId = new SelectList(db.Companies, "CompanyId", "Name"); 返回部分视图(); }

  2. Partialview代码:

@using Labkhand.Models
@model Labkhand.Models.Phase

@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()

    <div class="form-horizontal">
        @{
            //بدست آوردن نام شرکت
            LabkhandModel db = new LabkhandModel();
            string co = db.Users.FirstOrDefault(u => u.UserName == User.Identity.Name).Company.Name;
        }
        <h4>افزودن عنوان شغلی برای <strong class="text-danger">@co</strong></h4>

        @Html.ValidationSummary(true, "", new {@class = "text-danger"})

        <div class="row" id="EditPhasePanel">
            <div class="form-group">
                @Html.LabelFor(model => model.Name, htmlAttributes: new {@class = "control-label col-md-2"})
                <div class="col-md-10">
                    @Html.EditorFor(model => model.Name, new {htmlAttributes = new {@class = "form-control",
                   onkeypress = "return runScript(event)"}})
                    @Html.ValidationMessageFor(model => model.Name, "", new {@class = "text-danger"})
                </div>
            </div>
        </div>
        <div class="modal-footer">
            <input type="button" value="ذخیره" class="btn btn-danger btn-outline btnAddPhase"/>
            <button type="button" class="btn btn-primary closesPassModal" data-dismiss="modal" onclick="closeModal();">بستن</button>
        </div>

    </div>
    //Close modal
    <script type="text/javascript">
        function closeModal() {
                    document.getElementById('AddModal').style.display = "none";
        }
    </script>
}
  1. Modal 图片:

  2. 按下 Enter 键后:

【问题讨论】:

    标签: javascript asp.net-mvc


    【解决方案1】:

    我解决了。

    $(document).keypress(
        function(event){
         if (event.which == '13') {
            event.preventDefault();
          }
     
    });

    【讨论】:

      猜你喜欢
      • 2023-04-06
      • 1970-01-01
      • 1970-01-01
      • 2022-01-21
      • 2019-04-09
      • 1970-01-01
      • 1970-01-01
      • 2017-11-05
      • 1970-01-01
      相关资源
      最近更新 更多