为什么我在使用Modal中的PartialView中的输入键后进入PartialView?

时间:2018-03-16 11:06:07

标签: javascript asp.net-mvc

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

  1. PartialView中加载Modal
  2. 
    
    <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>
    &#13;
    <div id="AddModal" class="modal AddModal">
        <div class="modal-dialog modal-lg">
            <div class="well Addspan">
    
            </div>
    
        </div><!-- Add modal-dialog -->
    </div>
    &#13;
    &#13;
    &#13;

    1. PartialView中的Controller

      public ActionResult PhaseAdd()     {         ViewBag.CompanyId = new SelectList(db.Companies,&#34; CompanyId&#34;,&#34; Name&#34;);         返回PartialView();     }

    2. Partialview代码:
    3. &#13;
      &#13;
      @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>
      }
      &#13;
      &#13;
      &#13;

      1. Modal图片: Modal Image

      2. Enter 键后: After Press Enter key

1 个答案:

答案 0 :(得分:0)

我解决了。

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