jQuery 为它找到的每个元素返回两个元素?

发布于 2024-09-06 00:03:21 字数 3144 浏览 3 评论 0原文

首先我要说的是我对 jQuery 相当陌生。在大多数情况下,我发现它直观且强大,但这种情况让我彻底难住了。

在下面的方法中,对 .each() 的调用为每个找到的元素返回两个元素。它迭代一组给定 ID 的表行,这些 ID 以单词“通信”开头,后跟 ID 号。对于它返回的每一行,它都会处理两次。

使用 Firebug,我验证了 DOM 仅具有相关表行的单个实例。还使用 Firebug,我验证了该方法没有被调用两次; .each() 中的迭代实际上是对每个返回的表行进行两次。当所有 AJAX 调用完成后,我将在数据库中为表中创建的每一行创建两个条目。

这是导致问题的代码:

function getCommunications() {
    var list = $('[id^=communication]');
    var communications = new Array();
    list.each(function () {
        var communication = {
            ID: $(this).find('.commCompanyID').val(),
            /*
             * SNIP: more object properties here that are 
             * unnecessary to this discussion
             */
        };
        communications.push(communication);
    });
    return communications;
}

返回通信时,返回的数组将包含表行数两倍的元素。

我应该注意到,几乎相同的代码(但与特定的 div 列表相反)正在同一页面上运行。只是桌子遇到了问题。

我使用的是 jQuery 1.4.1,该版本随 Visual Studio .NET 2010 一起提供。

表标记是完全动态的——也就是说,除了标题行之外,它还依赖于页面加载时返回的数据或由用户通过对话框。我将只添加页面加载时创建的代码;再次使用 Firebug,我验证了当最终用户创建与对话框匹配的行时我动态创建的内容。 (任何人都应该可读,但郑重声明,这是一个 ASP.NET MVC 2.0 项目。)

<table id="commTable">
    <tr>
        <th></th>
        <th>
            Date / Time
        </th>
        <th>
            Contact
        </th>
        <th>
            Type
        </th>
        <th>
            Duration
        </th>
        <th>
            Notes
        </th>
    </tr>
<% foreach (var item in Model) { %>
    <tr id="communication<%: item.ID %>">
        <td>
            <a href="#" onclick="showEditCommunicationForm(<%: item.ID %>">
                Edit</a> 
            <span class="commDeleteButton">
                <a href="#" onclick="deleteCommunication(<%: item.ID %>)">
                    Delete</a>
            </span>
        </td>
        <td>
            <span class="commDateTime"><%: item.DateTime %></span>
            <input type="hidden" class="commID" value="<%: item.ID %>" />
            <input type="hidden" class="commIsDeleted" 
                value="<%: item.IsDeleted %>" />
        </td>
        <td>
            <span class="commSourceText"><%: item.Company.CompanyName %></span>
            <input type="hidden" class="commCompanyID" 
                value="<%: item.CompanyID %>" />
        </td>
        <td>
            <%: item.CommunicationType.CommunicationTypeText %>
            <input type="hidden" class="commTypeID" 
                value="<%: item.CommunicationTypeID %>" />
        </td>
        <td>
            <span class="commDuration"><%: item.DurationMinutes %></span> 
            Minutes
        </td>
        <td>
            <span class="commNotes"><%: item.Notes %></span>
        </td>
    </tr>    
<% } %>
</table>

I'll start by saying I'm fairly new to jQuery. For the most part, I've found it intuitive and powerful, but this one circumstance has me thoroughly stumped.

In the following method, the call to .each() returns two elements for every one found. It iterates over a set of table rows given IDs starting with the word, "communication," and followed by an ID number. For each row it returns, it processes twice.

Using Firebug, I've validated that the DOM only has a single instance of each table row in question. Also using Firebug, I've validated that the method is not being called twice; the iteration in .each() is truly going over each returned table row twice. By the time all the AJAX call goodness is done, I'll have two entries in the database for each row created in the table.

This is the code that's causing the issues:

function getCommunications() {
    var list = $('[id^=communication]');
    var communications = new Array();
    list.each(function () {
        var communication = {
            ID: $(this).find('.commCompanyID').val(),
            /*
             * SNIP: more object properties here that are 
             * unnecessary to this discussion
             */
        };
        communications.push(communication);
    });
    return communications;
}

At the point of return communications, the Array returned will contain twice as many elements as there are table rows.

I should note that nearly identical code (but going against specific lists of divs) is working on the same page. It's just the table that's suffering the issues.

I'm using jQuery 1.4.1, the version which shipped with Visual Studio .NET 2010.

The table markup is fully dynamic -- that is, aside from the header row, it's dependent on data either returned at page load or created by the user via a dialog box. I'll drop in just the code for what's created at page load; again using Firebug I've validated that what I create dynamically when an end user creates a row with the dialog box matches. (This should be readable by anyone, but for the record this is an ASP.NET MVC 2.0 project.)

<table id="commTable">
    <tr>
        <th></th>
        <th>
            Date / Time
        </th>
        <th>
            Contact
        </th>
        <th>
            Type
        </th>
        <th>
            Duration
        </th>
        <th>
            Notes
        </th>
    </tr>
<% foreach (var item in Model) { %>
    <tr id="communication<%: item.ID %>">
        <td>
            <a href="#" onclick="showEditCommunicationForm(<%: item.ID %>">
                Edit</a> 
            <span class="commDeleteButton">
                <a href="#" onclick="deleteCommunication(<%: item.ID %>)">
                    Delete</a>
            </span>
        </td>
        <td>
            <span class="commDateTime"><%: item.DateTime %></span>
            <input type="hidden" class="commID" value="<%: item.ID %>" />
            <input type="hidden" class="commIsDeleted" 
                value="<%: item.IsDeleted %>" />
        </td>
        <td>
            <span class="commSourceText"><%: item.Company.CompanyName %></span>
            <input type="hidden" class="commCompanyID" 
                value="<%: item.CompanyID %>" />
        </td>
        <td>
            <%: item.CommunicationType.CommunicationTypeText %>
            <input type="hidden" class="commTypeID" 
                value="<%: item.CommunicationTypeID %>" />
        </td>
        <td>
            <span class="commDuration"><%: item.DurationMinutes %></span> 
            Minutes
        </td>
        <td>
            <span class="commNotes"><%: item.Notes %></span>
        </td>
    </tr>    
<% } %>
</table>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

微凉 2024-09-13 00:03:21

alert($('[id^=communication]').length); 给你什么?您确定该表的祖先没有以通信开头的 ID 吗?

您可以使用 $('#commTable > tr[id^=communication]') 来非常具体

What does alert($('[id^=communication]').length); give you? Are you sure an ancestor of the table doesn't have an ID starting with communication?

You could use $('#commTable > tr[id^=communication]') to be extremely specific

我只土不豪 2024-09-13 00:03:21

我有两个建议——是否有帮助,谁知道呢。 :)

首先,使用更简洁的选择器,即 $("tr[id^=communication]")。这会快得多

其次,考虑只向 tr 元素添加一个类。这也会快得多。这需要将: 更改

<tr id="...">

为,例如

<tr id="..." class="comm-row">

这样您的 list 选择器就可以是 $("tr.comm-row")

对我来说,为什么会出现重复项并不是很明显,但也许上述建议不仅会加快代码速度,而且会提供一些关于重复发生原因的见解。

each 循环中设置断点可能会提供一些见解(即,this 在每次迭代中等于什么)。

希望有帮助。

I've two suggestions - whether they help, who knows. :)

First, use a more concise selector, i.e. $("tr[id^=communication]"). This will be much faster.

Second, consider just adding a class to your tr elements. This will also be much faster. This would entail changing:

<tr id="...">

to, e.g.

<tr id="..." class="comm-row">

That way your selector for list can be $("tr.comm-row").

It's not immediately obvious to me why you are getting duplicates, but perhaps the above suggestions will not only speed up the code but offer some insight into why duplicates are happening.

Setting up a breakpoint in the each loop may offer some insight (i.e. what does this equal on each iteration).

Hope that helps.

挽容 2024-09-13 00:03:21

尝试

var list = $('[id^=communication]').find('td');

try

var list = $('[id^=communication]').find('td');
赠佳期 2024-09-13 00:03:21

我不知道这是否能回答您的问题,但我在动态生成的表中遇到了类似的问题。

我正在使用 jQuery 可选择 UI,并根据状态标志制作了一个图像表。然而,我传递给 jQuery 来插入的 HTML 字符串与 Firefox 类似,它

<img src="images/imageIwant" class="classIwant"></img>

知道我的意思,并且只插入了一个图像标签。 看到类似“

<img ... />
</img/>

然而,在 IE (7, 8) 中,我在浏览器的开发人员工具中 检查 HTML”的内容。您插入的元素数量可能是您想象的两倍。

I don't know if this will answer your question, but i ran into a similar problem with dynamically generated tables.

I'm using the jQuery selectable UI, and made a table of images based on a status flag. However, the HTML string that I passed to jQuery to insert was something along the lines of

<img src="images/imageIwant" class="classIwant"></img>

Firefox knew what I meant, and only inserted one image tag. However, in IE (7, 8) I was seeing something like

<img ... />
</img/>

Check the HTML in the developer tools for your browser. It is possible you are inserting twice as many elements as you think.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文