0

我正在开发一个 MVC 应用程序。我在索引视图/表格中显示数据。

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

我试图在 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

1 回答 1

4

您应该包装 js 代码以准备好文档。

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

你需要使用不同idforeach. 例如

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

或使用 $(this).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);
        }
    });
})

于 2013-03-26T10:59:22.707 回答