2016-09-02 33 views
2

所以我有這個代碼。我想將我的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); 
    } 
+3

'的.html()','的.text()'... –

+0

如果你給'name'屬性您他們會自動發佈的輸入,但是你的控制器將不得不改變,你將不得不提出一個命名策略,因爲它們是在循環中創建的。 – Crowcoder

回答

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>

+0

這段代碼的問題在於,布爾值只返回「on」而不是「true/false」 –

+0

如果應用.serialize(),則布爾值完全打開/關閉。無論如何,將這些值轉換爲真/假並不困難。謝謝 – gaetanoM

+0

他們沒有成爲或他們總是上 –

0

建議使用你的周圍輸入的<form id="formID" ...>標籤,並在阿賈克斯方法使用$("#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(); 
         } 
        }); 
1

您可以通過.html()訪問的值。正如Vijai所說的,假設你有一個結構化的表單,你可以使用serialize來使它成爲一個POST數組,如(& =等)格式。或者,爲了保持與原始代碼類似,您可以像平常一樣傳遞JSON。像

data:{shippingInfo: "value", moreInfo: "blahblah"} 
相關問題