【问题标题】:Iterating through generic collection using jquery or javascript使用 jquery 或 javascript 遍历泛型集合
【发布时间】:2014-03-06 10:17:01
【问题描述】:

我有一个动态创建表结构的视图

<div>
        @if (Model.parameterListForReviewerOne != null)
        {
            foreach (var item in Model.parameterListForReviewerOne)
            {
                if (item.parmID != null && item.parmID != 0)
                {
            <tr class="highlightRed parameterOfReviewer1 bluebgtable-confi">
                <td style="display: none">@Html.HiddenFor(modelItem => item.parmID)
                </td>
                <td align="center" width="25%">
                    @Html.DisplayFor(modelItem => item.ParameterDesc, new { @readonly = "readonly" })
                </td>
                <td align="center" class="one" width="35%">
                    @*@Html.TextBoxFor(modelItem => item.Reviewer1Ratings, new { @class = "ReviewerOneClass", @id = "Ratings1_" + @item.parmID })*@
                    @Html.DropDownListFor(model => item.Reviewer1Ratings, new SelectList(Model.RatingsList, item.Reviewer1Ratings), new { @class = "ReviewerOneClass HideinPrint", @id = "Ratings1_" + @item.parmID })
                    <span class="ShowinPrint" id="forPrintRatings1_@item.parmID">dummy</span> <span style="color: #E80C4D; display: none;" class="hide" id="span_Reviewer1Rating_@item.parmID">
                        Invalid range.</span> <span style="color: #E80C4D; display: none;" class="hide" id="span_Reviewer1RatingRequired_@item.parmID">
                            Required</span>
                </td>
                <td align="center" class="one" height="80px" width="40%">
                    @Html.TextAreaFor(modelItem => item.Reviewer1Comments, new { @class = "ReviewerCommentsClass requiredField disableReviewer1 reviewer1ParaFieldLimit HideinPrint", @id = "Comments1_" + @item.parmID })
                    <div class="ShowinPrint" id="Comments1_@item.parmID">
                        dummy</div>
                    <span style="color: #E80C4D; display: none;" class="hide hideComments1_@item.parmID" id="span_Reviewer1CommentsRequired_@item.parmID">
                        Required</span> <span id="spn_Comments1_@item.parmID" class="classSec3FieldLimit" style="display: none">
                        </span>
                </td>
            </tr>
                }
            }
        }
        <tr class="bluebgtable-confi">
            <td align="center" width="25%">
                Overall Ratings
            </td>
            <td align="center" id="OverallRating1" valign="middle" height="60px" width="35%">
                @* @Html.TextBoxFor(model => model.Reviewer1OverallRating)*@
                @Html.DropDownListFor(model => model.Reviewer1OverallRating, new SelectList(Model.RatingsList), new { @class = "HideinPrint" })
                <span class="ShowinPrint" id="forPrintReviewer1OverallRating">dummy</span> <span
                    style="color: #E80C4D; display: none;" class="hide" id="span_OverAllRatingReviewer1">
                    Invalid range.</span> <span style="color: #E80C4D; display: none;" class="hide" id="span_OverAllRatingReviewer1Required">
                        Required</span>
            </td>
            <td id="OverallRating1" align="center" class="requiredField" height="80px" width="40%">@Html.TextAreaFor(model => model.Reviewer1OverallRatingComments, new { @class = "disableReviewer1 HideinPrint" })
                <div class="ShowinPrint" id="forPrintReviewer1OverallRatingComments">
                    dummy</div>
                <p>@Html.ValidationMessageFor(model => model.Reviewer1OverallRatingComments)</p>
                <span style="color: #E80C4D; display: none;" class="hide hideOverallRating1" id="span_Reviewer1OverallRatingCommentsRequired">
                    Required</span>
            </td>
        </tr>
    </div>

在 javascript 函数中我想获取动态生成的 parmID 如何在 javascript 或 jquery 中获取 parmID。实际上我想将代码复制到将隐藏的 div 我想获取动态生成的 id。

【问题讨论】:

    标签: jquery asp.net-mvc view generic-collections


    【解决方案1】:

    在 MVC4 中,您可以编写 &lt;tr id="@Html.IdFor(modelItem =&gt; item.parmID)" ...,然后使用 jquery 选择这些 id。 或生成一些javascript,即

    <script>
     var id = '@Html.IdFor(modelItem => item.parmID)';
    </script>
    

    或在 tr &lt;tr class='myclass'&gt; 上设置属性或类,然后使用 $('.myclass').forEach(...)

    【讨论】:

      【解决方案2】:

      首先编辑您的tr 以存储其parmID。喜欢:

      <tr data-parmID="@item.parmID" class="highlightRed parameterOfReviewer1 bluebgtable-confi">
      .
      .
      .
      </tr>
      

      在你可以迭代它们之后

      $("tr").each(function() {
          var parmID = $(this).attr("data-parmID");
          // do your work here
      });
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-11-02
        • 2014-05-26
        • 2015-12-21
        • 1970-01-01
        • 2010-11-17
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多