在Jquery / javascript中传递属性不起作用?

时间:2015-11-29 17:55:18

标签: javascript jquery

    $(function () {
        $(".itemsTable").on("click", "#addClaimItem", function() {
            debugger;
            var tableName = $(".itemsTable").attr('name').toString();
            PopulateTable(tableName, null, null);    //tableName = "CorrectValue"
        });
    });

    function PopulateTable(tableName, baggageItems, isuneditableInEnquiryMode) {
       var table = $("#" + tableName + " table");  //tableName = null
        debugger;
        var id = table.attr("data-id");
...code continues

<div id="BagList">
    <div class="fieldBox">
        <div class="field">
            <table data-id="@(Model.Trav.BagPage.BagList.Count())" class="itemsTable" name="BagList">
                <thead>
                    <tr>
                        <th>Description</th>
                        <th>Age</th>
                        <th>Receipt</th>
                        <th>Cost</th>
                        <th>Present Value</th>
                        <th>Amount Claimed</th>
                        <th></th>
                    </tr>
                </thead>
                <tbody>
                    @for (int i = 0; i < Model.Trav.BagPage.BagList.Count(); i++)
            {
                        <tr>
                            <td>@Html.TextBoxFor(x => x.Trav.BagPage.BagList[i].BagDescription, new {@class = "tableData-input editableInNew editableInUpdate itemDescription", @readonly = ""})</td>
                            <td>@Html.TextBoxFor(x => x.Trav.BagPage.BagList[i].BagAge, new {@class = "tableData-input editableInNew editableInUpdate age", @readonly = ""})</td>
                            <td>@Html.CheckBoxFor(x => x.Trav.BagPage.BagList[i].BagReceipt, new {@class = "tableData-input editableInNew editableInUpdate receipt", @readonly = ""})</td>
                            <td>@Html.TextBoxFor(x => x.Trav.BagPage.BagList[i].BagCost, new {@class = "tableData-input editableInNew editableInUpdate currencyAmount", @readonly = ""})</td>
                            <td>@Html.TextBoxFor(x => x.Trav.BagPage.BagList[i].BagPresentValue, new {@class = "tableData-input editableInNew editableInUpdate currencyAmount", @readonly = ""})</td>
                            <td>@Html.TextBoxFor(x => x.Trav.BagPage.BagList[i].BagAmountClaimed, new {@class = "tableData-input editableInNew editableInUpdate currencyAmount", @readonly = "" })</td>
                            <td>
                                <div class="removeClaimItem minus editableInNewSymbol editableInUpdateSymbol disabled">
                                    @Html.Hidden(Html.NameFor(x => x.Trav.BagPage.BagList[i].MarkedAsDeleted).ToString(), Model.Trav.BagPage.BagList[i].MarkedAsDeleted.ToString().ToLower())
                                </div>
                            </td>
                        </tr>
            }
                    <tr class="plusRow">
                        <td></td>
                        <td></td>
                        <td></td>
                        <td></td>
                        <td></td>
                        <td></td>
                        <td>
                            <div id="addClaimItem" class="plus editableInNewSymbol editableInUpdateSymbol disabled"></div>
                        </td>
                    </tr>
                </tbody>
            </table>
        </div>
    </div>
</div>

为什么tableName变量在函数本身中为null?我无法在任何地方找到一个回答(我必须是一个菜鸟)尝试了一些变化,但难倒!

编辑:在Chrome元素检查器中,给出了javascript / Jquery值,请参阅给定的值// tableName = value 。 var tableName最初设置为&#34; bagItems&#34;但是当PopulateTable函数接收到tableName时,它被设置为null ???????? 表格标签中没有id但是还有一个id =&#34; BagList&#34;进一步提升表的祖先