所以我有這個代碼。我想將我的html輸入內的數據發送到控制器參數。在我的jQuery中我有一條線,看起來像這樣
var currentRow = $(this).closest(「tr」); 但我不知道如何進入currentRow對象內的每個輸入的值如何做到這一點,或者你也許知道更好的方法來做到這一點?我怎樣才能進入一個HTML <tr>對象的值
// my html
@foreach (var discount in Model.DiscountList)
{
<tr>
<td>@Html.TextBox("codeTextBox", discount.Code) </td>
<td><input type="checkbox" name="freeShippingCheckBox" id="freeShippingCheckBox" checked="@discount.FreeShipping" /> </td>
@if (discount.isTwoForThree == true)
{
<td><input type="tel" value="Ta 3 betala för 2" readonly /></td>
}
else
{
<td><input type="number" value="@discount.PercentOffPrice"/></td>
}
<td><input type="checkbox" id="activeCheckBox" name="activeCheckBox" checked="@discount.Active" /></td>
<td><input type="datetime" value="@discount.CreatedDate" readonly /></td>
<td>
<input type="button" value="Radera" class="fa fa-remove" data-url="@Url.Action("DeleteDiscountCode","Discount",new { id= discount.Id})" />
<input type="button" value="Uppdatera" class="fa fa-update" data-url="@Url.Action("UpdateDiscount","Discount",new { id= discount.Id, })" />
<input type="button" value="Produkter" class="fa fa-products" />
</td>
<input id="@discount.Id.ToString()" type="hidden" value="@discount.Id" />
</tr>
}
//my jquery
$(".fa-update").on("click", function() {
var currentRow = $(this).closest("tr");
console.log(currentRow.children("#freeShippingCheckBox").val());
if (confirm("Are you sure you want to edit this discount code?"))
{
$.post($(this).data("url"), function (data) {
$.ajax({
url: '@Url.Action("DeleteUser","Discount")',
type: "POST",
data: "freeShipping=..........???????????",
success: function (data) {
if (data) {
}
location.reload();
}
});
location.reload();
alert("Deleted");
})
}
});
//my controller
[HttpPost]
public void UpdateDiscount(int id, bool freeShipping, int percentOfPrice, bool active)
{
Service.DiscountService.UpdateDiscount(id, freeShipping, percentOfPrice, active);
}
'的.html()','的.text()'... –
如果你給'name'屬性您他們會自動發佈的輸入,但是你的控制器將不得不改變,你將不得不提出一個命名策略,因爲它們是在循環中創建的。 – Crowcoder