JQuery each():index始终为0

时间:2015-12-08 19:13:55

标签: javascript jquery asp.net-mvc datatables

我有一个jquery每个()嵌套在Datatables 操作函数中。我试图获取已检查行的值(td),因此我想我使用了传递给 each()函数的索引并使用索引来选择行(tr)。指数始终为0.我的方法似乎存在缺陷,但这只是一个开始。我需要检查行中的值。

var data = hesTable.$('input[type="checkbox"]:checked').each(function (index, element) {
                        var item = $(this);
                        alert(index);
                        alert('Row index: ' + item.index())
                        alert('Row index: ' + item.parent().index());
});

HTML(MVC)

<tbody>
            @if (Model.HesViewModels != null)
            {
                // Can't use the DisplayTemplate here because need to assign Data-* attributes
                //
                //foreach (var item in Model.HesViewModels)
                for (var i = 0; i < Model.HesViewModels.Count; i++)
                {
                    // Make sure Hes Service returns data
                    //
                    if (Model.HesViewModels[i] != null)
                    {
                        <tr>
                            <td>
                                @Html.CheckBoxFor(s => Model.HesViewModels[i].Process,
                                  new
                                  {                                      
                                      data_detailid = Model.HesViewModels[i].PartnerReferralDetailId,
                                      data_reasoncode = Model.HesViewModels[i].ReferralDetailReasonCode,
                                      data_statuscode = Model.HesViewModels[i].ReferralDetailStatusCode,
                                      data_outcomecode = Model.HesViewModels[i].ReferralDetailOutcomeCode
                                  })
                            </td>
                            <td>@Model.HesViewModels[i].PartnerReferralDetailId</td>
                            <td>@Html.TextBoxFor(m => Model.HesViewModels[i].ReferralDetailStatusCode)</td>
                            <td>@Html.TextBoxFor(m => Model.HesViewModels[i].ReferralDetailReasonCode)</td>
                            <td>@Html.TextBoxFor(m => Model.HesViewModels[i].ReferralDetailOutcomeCode)</td>
                        </tr>
                    }
                }
            }
        </tbody>

RENDERED HTML

<input data-detailid="1578441" data-outcomecode="154" data-reasoncode="9" data-statuscode="8" data-val="true" data-val-required="The Process field is required." id="HesViewModels_0__Process" name="HesViewModels[0].Process" type="checkbox" value="true" /><input name="HesViewModels[0].Process" type="hidden" value="false" />

2 个答案:

答案 0 :(得分:1)

Try using selector import poser import os scene = poser.Scene() pathname = os.path.split(poser.AppLocation())[0] pathname = os.path.join(pathname, 'Runtime', 'Python', 'poserScripts', 'ScriptsMenu', 'GadgetGirl') sys.path.append(pathname) try: import SelectMultiple except: print "Could not import SelectMultiple script" def ChoiceWindow(): title = "SuperFly Node Fixer" message = "Choose the operation to preform" list_of_operations = ["Delete Node", "Detach Node", "Re-Link Node"] drop_down_window = poser.DialogSimple.AskMenu(title, message, list_of_operations) return drop_down_window def Controller(): script_to_run = ChoiceWindow() #Need to call multiple so that we can know on what figures list_of_figures = SelectMultiple.MyApp() print type(list_of_figures) Controller() to select only "table tr:has(:checked)" which have tr element where input is checked . Approach iterates true elements instead of tr elements , to avoid need to select parent elements within input

.each()
$("table tr:has(:checked)").each(function(index, element) {
  console.log($(this).index())
})

答案 1 :(得分:-1)

因为index()基于兄弟姐妹。不是页面上的所有复选框。所以使用行的索引。

item.closest("tr").index()

如果你想获得tds,那么你真的不需要索引()

item.closest("tr").find("td")