Skip to content Skip to sidebar Skip to footer

How Can I Get Into The Value Inside Of An Html Object

So i have this code. I want to send the data inside of my html inputs to the controllers parameters. In my jquery i have a line that looks like this var currentRow = $(this).closes

Solution 1:

My proposal is:

$(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//
  });
});
<scriptsrc="https://code.jquery.com/jquery-1.12.4.min.js"></script><table><tbody><tr><td>@Html.TextBox("codeTextBox", discount.Code) </td><td><inputtype="checkbox"name="freeShippingCheckBox"id="freeShippingCheckBox"checked="@discount.FreeShipping" /></td><td><inputtype="tel"value="Ta 3 betala for 2"readonly /></td><td><inputtype="checkbox"id="activeCheckBox"name="activeCheckBox"checked="@discount.Active" /></td><td><inputtype="datetime"value="@discount.CreatedDate"readonly /></td><td><inputtype="button"value="Radera"class="fa fa-remove"data-url="@Url.Action("DeleteDiscountCode","Discount",new { id= discount.Id})" /><inputtype="button"value="Uppdatera"class="fa fa-update"data-url="@Url.Action("UpdateDiscount","Discount",new { id= discount.Id, })" /><inputtype="button"value="Produkter"class="fa fa-products" /></td><inputid="@discount.Id.ToString()"type="hidden"value="@discount.Id" /></tr></tbody></table>

Solution 2:

You can access a value via .html(). As Vijai said, you can use serialize to make it a POST array like (&=, etc) format assuming you have a structured form. Or, to stay similar to your original code, you can just pass as usual JSON. like

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

Solution 3:

It is recommended to use a <form id="formID" ...> tag around yours inputs and use $("#formID").serialize() in the Ajax method if you are not doing processing of the input value in the View. Refer this sample link : 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();
                        }
                    });

Post a Comment for "How Can I Get Into The Value Inside Of An Html Object"