2017-02-09 46 views
-1

我試圖凌駕於提交行爲<form>,所以我用下面的代碼:jQuery的提交()是不是叫

<script type="text/javascript"> 
    $(function() { 
     var form = $("#FilterForm"); 

     form.submit(function() { 
      alert("Submit"); 

      $.ajax({ 
       url: form.attr("action"), 
       type: 'POST', 
       data: new FormData(this), 
       processData: false, 
       contentType: false, 
       success: function (response) { 
        alert(response); 
        $('#Container').html(response); 
       }, 
       error: function() { 
        $('#Container').html(loadingFailedStr); 
       }, 
       timeout: 15000 
      }); 

      return false; 
     }) 
    }); 
</script> 

此腳本塊位於我的表後。這種形式的局部視圖聲明:

<div class="panel card-0"> 
    <div class="panel-body"> 
     @using (Html.BeginForm("TasksGridViewPartial", "Tasks", new { StaffId = staffId, StageId = stageId }, FormMethod.Post, new { id = "FilterForm", enctype = "multipart/form-data" })) 
     { 
      <fieldset> 
       <div class="form-group row"> 
        <div class="col-md-2 col-sm-6"> 
         @Html.Label(Headers.DateCreate) 
         <div class="datepicker"> 
          @Html.TextBoxFor(m => m.CreatedOn1, new { @class = "form-control" }) 
         </div> 
         <div class="datepicker"> 
          @Html.TextBoxFor(m => m.CreatedOn2, new { @class = "form-control" }) 
         </div> 
        </div> 
        <div class="col-md-2 col-sm-6"> 
         @Html.Label(Headers.DateClose) 
         <div class="datepicker"> 
          @Html.TextBoxFor(m => m.ClosedOn1, new { @class = "form-control" }) 
         </div> 
         <div class="datepicker"> 
          @Html.TextBoxFor(m => m.ClosedOn2, new { @class = "form-control" }) 
         </div> 
        </div> 
        <div class="col-md-2 col-sm-6"> 
         @Html.Label(Headers.DateStart) 
         <div class="datepicker"> 
          @Html.TextBoxFor(m => m.StartDate1, new { @class = "form-control" }) 
         </div> 
         <div class="datepicker"> 
          @Html.TextBoxFor(m => m.StartDate2, new { @class = "form-control" }) 
         </div> 
        </div> 
        <div class="col-md-2 col-sm-6"> 
         @Html.Label(Headers.DateEnd) 
         <div class="datepicker"> 
          @Html.TextBoxFor(m => m.EndDate1, new { @class = "form-control" }) 
         </div> 
         <div class="datepicker"> 
          @Html.TextBoxFor(m => m.EndDate2, new { @class = "form-control" }) 
         </div> 
        </div> 
        <div class="col-md-2 col-sm-6"> 
         @Html.Label(Headers.DateCheck) 
         <div class="datepicker"> 
          @Html.TextBoxFor(m => m.VerifiedOn1, new { @class = "form-control" }) 
         </div> 
         <div class="datepicker"> 
          @Html.TextBoxFor(m => m.VerifiedOn2, new { @class = "form-control" }) 
         </div> 
        </div> 
        <div class="col-md-2 col-sm-6"> 
         @Html.Label(Headers.TaskNumber) 
         <div style="padding: 4px;"> 
          @Html.TextBoxFor(m => m.VerifiedOn1, new { @class = "form-control", type = "number" }) 
         </div> 
         <div style="padding: 4px;"> 
          @Html.TextBoxFor(m => m.VerifiedOn2, new { @class = "form-control", type = "number" }) 
         </div> 
        </div> 
       </div> 
       <div class="form-group row"> 
        <div class="col-md-3 col-sm-6"> 
         @Html.LabelFor(m => m.Executors) 
         @Html.ListBoxFor(m => m.Executors, executors, new { @class = "selectpicker form-control", @title = Messages.ChooseExecutors, multiple = "" }) 
        </div> 
        <div class="col-md-3 col-sm-6"> 
         @Html.LabelFor(m => m.Projects) 
         @Html.ListBoxFor(m => m.Projects, projects, new { @class = "selectpicker form-control", @title = Messages.ChooseProjects, multiple = "" }) 
        </div> 
        <div class="col-md-3 col-sm-6"> 
         @Html.LabelFor(m => m.Statuses) 
         @Html.ListBoxFor(m => m.Statuses, statuses, new { @class = "selectpicker form-control", @title = Messages.ChooseStatuses, multiple = "" }) 
        </div> 
       </div> 
       <div class="form-group row" style="padding-top: 24px;"> 
        <div class="col-md-12"> 
         <button type="submit" class="btn btn-primary">@Actions.Apply</button> 
         <button type="button" class="btn btn-default">@Actions.ClearFilters</button> 
         <button type="button" class="btn btn-default">@Actions.ShowAccepted</button> 
         <button type="button" class="btn btn-default">@Actions.HideAccepted</button> 
        </div> 
       </div> 
      </fieldset> 
     } 
    </div> 
</div> 

我打電話渲染這個@Html.Action("FilterPartial")局部視圖。
最有趣的是,我檢查了form變量在文檔就緒事件,它被宣佈爲需要。但submit仍然沒有被調用。
問題在哪裏?

+0

你肯定* *的形式是頁面中的「準備」事件觸發時的一部分嗎?如果不是,你不會得到任何錯誤。你有沒有嘗試過代表團的綁定? – Pointy

+0

我在ready函數中記錄了這個變量,它被定義了,而不是null。 –

+0

您是否有另一個具有相同ID的表單(或任何其他元素)? –

回答

1

return false是不夠的,還需要阻止默認:

$(function() { 
     var form = $("#FilterForm"); 

     form.submit(function (e) { // Pass the event to the function 
      e.preventDefault(); // Stop the form submitting. 

      $.ajax({ 
       url: form.attr("action"), 
       type: 'POST', 
       data: new FormData(this), 
       processData: false, 
       contentType: false, 
       success: function (response) { 
        alert(response); 
        $('#Container').html(response); 
       }, 
       error: function() { 
        $('#Container').html(loadingFailedStr); 
       }, 
       timeout: 15000 
      }); 

      return false; 
     }) 
    }); 
+2

從事件處理函數返回false將自動調用'event.stopPropagation()'和'event.preventDefault()'。 – Pointy

+0

謝謝,當然!但我的問題不是這個。它不是絕對的調用,所以函數體中的所有代碼都沒有任何值。 –

+0

在*未調用*的事件處理函數上調用preventDefault將沒有任何影響! –