2013-03-26 34 views
0

我正在开发一个MVC应用程序。 我正在索引视图/表中显示数据。如何在MVC 3视图中的JS中获取模型项数据?

我在最后一列添加了额外的复选框。 当用户点击该复选框时,我想从该选定/检查行的特定列中获取值。

我想在JS的警报窗口中显示该值,但它不显示。

如何做到这一点。

@model PagedList.IPagedList<PaymentAdviceEntity.PaymentAdvice> 

      <table class="table table-striped"> 
       <tr> 
        <th>Company 
        </th> 
        <th> 
         Amount 
        </th> 
       </tr> 

     @foreach (var item in Model) 
       { 
        <tr> 
         <td> 
          @Html.DisplayFor(modelItem => item.Company.Name) 
         </td> 
         <td> 
          @Html.DisplayFor(modelItem => item.SanctionedAmount,"sAmount","sAmount") 
         </td> 
         <td> 

    @Html.CheckBox("IsPaid", (bool)item.IsPaid, new { @value=item.Id,@class="IsPaid-"+item.Id}) 

        </td> 
        </tr> 
    </table> 

<script type="text/javascript"> 

      $("input[type='checkbox']").click(function() { 

       if ($(this).is(":checked")) 
       { 
        var nPaid = $("#sAmount").val(); 
        alert(nPaid); 
       } 

       }); 
     </script> 

回答

4

你应该将js代码包装到文档就绪。

<script type="text/javascript"> 
     $(function(){ 
     //TO DO smt 
     }); 
     }); 
    </script> 

而且你需要在foreach使用不同id。例如

@Html.DisplayFor(modelItem => item.SanctionedAmount, string.Format("sAmount{0}", item.Id),"sAmount") 

,或者你使用$(本).parent jQuery选择更改...

的所有代码将是:

@model PagedList.IPagedList<PaymentAdviceEntity.PaymentAdvice> 

     <table class="table table-striped"> 
      <tr> 
       <th>Company 
       </th> 
       <th> 
        Amount 
       </th> 
      </tr> 

    @foreach (var item in Model) 
      { 
       <tr> 


        <td> 
         @Html.DisplayFor(modelItem => item.Company.Name) 
        </td> 

        <td> 
         @Html.DisplayFor(modelItem => item.SanctionedAmount,"sAmount",new {id = string.Format("sAmount{0}", item.Id)}) 
        </td> 


        <td> 

@Html.CheckBox("IsPaid", (bool)item.IsPaid, new { @value=item.Id,@class="IsPaid-"+item.Id}) 


       </td> 
       </tr> 
</table> 


<script type="text/javascript"> 
$(function() { 
    $("input[type='checkbox']").click(function() { 
     if ($(this).is(":checked")) { 
      var v = parseInt($(this).val(), 10); 
      var s = 1; 
      var nPaid = $("#sAmount" + v).val(); 
      alert(nPaid); 
     } 
    }); 
}) 

+0

在所有 – Nil 2013-03-26 11:10:15

+0

PLS不工作,等待3分钟我会改变你的代码 – user571874 2013-03-26 11:14:12

+0

这是正确的代码?我不这么认为 – Nil 2013-03-26 11:22:28

相关问题