[英]Jquery $(this).closest('form'); not working after button click
[英]jQuery get closest form on button click
我想在按下表單按鈕時發出ajax請求我需要獲取表單本身我的代碼是
$(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;
});
$(this).closest('form') 在頁面中獲取另一個表單我不知道為什么
這是我要找的表格
<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>
$(this).closest('form')
沒有得到另一種形式。 它實際上沒有得到任何形式。
我不知道為什么.closest('form')
在那種情況下不起作用
無論如何,這行代碼對我有用
var form = $(this).parent().parent().children(":first");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.