2017-07-31 57 views
0

我希望當我按下表單按鈕我需要得到窗體本身做出一個Ajax請求 我的代碼是jQuery的獲取按鈕最接近的形式點擊

$(document).ready(function() { 
     $(document).on('click', '.finalEdit', function (e) { 
      e.preventDefault(); 
      var form = $(this).closest('form'); 
      $.ajax({ 
       data: form.serialize(), 
       url: form.attr('action'), 
       type: "POST", 
       dataType: "html", 
       success: function (result) { 
        // feel free to execute any code 
        // in the success callback 
        $('#divToRefresh').html(result); 
       }, 
       error: function (result) { 
       alert("Failed"); 
      } 
      }) 

      return false; 
     }); 

$(本).closest( 「形」)獲取頁面的另一種形式,我不知道爲什麼

這是我期待的

<tr hidden="hidden"> 
      @using (Html.BeginForm("Edit", "Home", FormMethod.Post, new { @class = "Edit-form" })) 
      { 
       @Html.ValidationSummary(true) 
       <td> 
        @Html.HiddenFor(model => model.supplier.SupplierID, new { @Value = item.SupplierID }) 
       </td> 
       <td> 
        @Html.TextBoxFor(model => model.supplier.SupplierName, new { @Value = item.SupplierName }) 
       </td> 
       <td> 
        <input type="submit" value="Edit" class="finalEdit"/> 
       </td> 



      } 
     </tr> 
+0

工作嘗試'e.target'與'this'爲'本'可能指的是文檔與點擊元素。正如'var form = $(e.target).closest('form');' – Forty3

+1

它不起作用。 –

+0

哪種形式越來越好?從顯示的代碼中,只有一種形式。如果你有一個窗體內的表單,然後[它是不是有效的HTML](https://stackoverflow.com/questions/555928/is-it-valid-to-have-a-html-form-inside-another -html形式)。您是否驗證了呈現的HTML的完整性? –

回答

0

$(this).closest('form')沒有得到一個形式其他形式。它實際上沒有得到任何形式。

我不知道爲什麼.closest('form')沒有在那種情況下

反正此行代碼爲我工作

var form = $(this).parent().parent().children(":first");