使用 jQuery 编辑添加到表中的行

发布于 2024-11-16 16:59:14 字数 1088 浏览 3 评论 0原文

我找到了这篇文章: 编辑和删除新添加的表row 使用 Jquery ,这似乎正是我正在寻找的,但我无法让他们建议的解决方案发挥作用。

进行 ajax 调用后,我向表中添加一个新行。该行看起来与此类似:

<tr class="classRow" bgcolor="#EFE5D3" style="font-weight: bold; font-size: 1.1em;">
    <td width="35px"><a class="classEditLink classAdminFormSubmit" name="33" href="#">Edit</a></td>
    <td width="20px"><input type="checkbox" class="chkDeleteClass" name="deleteClasses[]" value="33" /></td>
    <td>CLASS101</td>
    <td>Class Description</td>
</tr>

“编辑”链接只有在页面刷新后才起作用(我假设这与它在添加到表后并未立即出现在 DOM 中有关)。因此,我需要找到一种解决方法,这使我看到了上述帖子。

我从那里修改了代码,如下所示:

$('a').live('click', function(e){
    if ($(e.target).attr('class') == 'classAdminFormSubmit') {
        alert($(e.target).name());

        OpenEditDialog($(this));
        return false;
    }
});

警报永远不会触发,所以我希望这只是我的选择器需要调整。我需要将选定的锚标记传递给 OpenEditDialog 函数。

I found this post: Editing and deleting a newly added table row using Jquery and that seemed like it was exactly what I was looking for, but I couldn't get their suggested solution to work.

After making an ajax call, I add a new row to a table. The row looks similar to this:

<tr class="classRow" bgcolor="#EFE5D3" style="font-weight: bold; font-size: 1.1em;">
    <td width="35px"><a class="classEditLink classAdminFormSubmit" name="33" href="#">Edit</a></td>
    <td width="20px"><input type="checkbox" class="chkDeleteClass" name="deleteClasses[]" value="33" /></td>
    <td>CLASS101</td>
    <td>Class Description</td>
</tr>

The Edit link doesn't work until after a page refresh (I'm assuming that has something to due with the fact that it's not in the DOM immediately after it's added to the table). So, I needed to find a workaround, which lead me to the above-mentioned post.

I modified the code from there to look like this:

$('a').live('click', function(e){
    if ($(e.target).attr('class') == 'classAdminFormSubmit') {
        alert($(e.target).name());

        OpenEditDialog($(this));
        return false;
    }
});

The alert never fires, so I'm hoping it's just that my selector needs adjusting. I need to pass the selected anchor tag to the OpenEditDialog function.

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

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

发布评论

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

评论(2

屋檐 2024-11-23 16:59:14
$( 'a' ).live( 'click', function () {

    var $this = $( this );

    if ( $this.hasClass( 'classAdminFormSubmit' ) ) {

        alert( $this.attr( 'name' ) );

        OpenEditDialog( $this );

        return false;

    }

});
$( 'a' ).live( 'click', function () {

    var $this = $( this );

    if ( $this.hasClass( 'classAdminFormSubmit' ) ) {

        alert( $this.attr( 'name' ) );

        OpenEditDialog( $this );

        return false;

    }

});
心不设防 2024-11-23 16:59:14

首先,在刚刚添加的行中放置一个链接,并为其指定一个类,假设它是 editRow
其次,使用此代码:

$('a.editRow').live('click', function(e){

    alert($(e.target).name());

    OpenEditDialog($(this));
    e.preventDefault(); // this is better than return false;
});

First, put a link in the row you just added, and give it a class, let's say it's editRow.
Second, use this code:

$('a.editRow').live('click', function(e){

    alert($(e.target).name());

    OpenEditDialog($(this));
    e.preventDefault(); // this is better than return false;
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文