我怎样才能进入html对象的内部值

时间:2016-09-02 18:31:23

标签: javascript c# jquery html asp.net-mvc

所以我有这个代码。我想将我的html输入内的数据发送到控制器参数。在我的jquery中,我有一条看起来像这样的线 var currentRow = $(this).closest(" tr"); 但我不知道如何进入currentRow对象中每个输入的值我该怎么做呢或者你知道更好的方法吗?

 // 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);
    }

3 个答案:

答案 0 :(得分:1)

您可以通过.html()访问值。正如Vijai所说,假设您有一个结构化的表单,您可以使用序列化使其成为一个类似(&amp; =等)格式的POST数组。或者,为了保持与原始代码类似,您可以像往常一样传递JSON。喜欢

data:{shippingInfo: "value", moreInfo: "blahblah"}

答案 1 :(得分:1)

我的建议是:

$(function () {
  $(".fa-update").on("click", function (e) {
    var data = {};
    var currentRowParams = $(this).closest("tr").find('input').each(function(index, element) {
      var name = element.name ? element.name : 'undefined' + index;
      data[name] = element.value || '';
    });
    var x =  JSON.stringify(data);
    console.log(x);
    
    //
    // If instead you want the params in a traditional GET
    //
  });
});
<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>

<table>
    <tbody>
    <tr>
        <td>@Html.TextBox("codeTextBox", discount.Code) </td>
        <td><input type="checkbox" name="freeShippingCheckBox" id="freeShippingCheckBox" checked="@discount.FreeShipping" /> </td>
        <td><input type="tel" value="Ta 3 betala for 2" readonly /></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>
    </tbody>
</table>

答案 2 :(得分:0)

如果您没有在View中处理输入值,建议在您的输入周围使用<form id="formID" ...>标记,并在Ajax方法中使用$("#formID").serialize()。请参阅此示例链接:Submit form using AJAX and jQuery

$.post($(this).data("url"), function (data) {
                    $.ajax({
                        url: '@Url.Action("DeleteUser","Discount")',
                        type: "POST",
                        data: $("#formID").serialize(),
                        success: function (data) {
                            if (data) {

                            }
                            location.reload();
                        }
                    });