JQuery 可重用函数

发布于 2024-07-17 20:21:34 字数 1070 浏览 6 评论 0原文

对 JQuery 完全陌生,所以我每天都在学习。

我注意到的一件事是它是多么容易,你可以写

$('div#test).remove();

但我正在寻找一个关于如何重用某些代码的示例,例如:

function RemoveTableRow(row, id) 
{
   $(row).remove();
   // id should be used for ajax call
}

然后在我的锚标记上添加一个“onclick”

onclick="RemoveTableRow('user-row-1', 32);"

但不知何故它不起作用,即使我将其添加到 document.ready 函数中。 有人可以帮我剪掉它,这是执行此操作的最佳实践方法吗?

提前致谢。 这个论坛真是杀手啊!

更新

我从此处获得的帮助中更新了代码。 这是我当前的代码,我想知道这是否是执行此操作的最佳方法。

function RemoveTableRow(row, id) {
            $.ajax({
             type: "POST",
             url: "Default.aspx/DeleteEmployee",
             data: "{'ID':'" + id + "'}",
             beforeSend: function() {
                  $("#" + row).animate({'backgroundColor':'#fb6c6c'},300);
             },
             success: function() {
              $("#" + row).slideUp(300,function() {
                    $("#" + row).remove();
              });
             }
        });
    }

Completely new to JQuery so I am learning everyday.

One thing I noticed is how easy it is, you can just write

$('div#test).remove();

But I am looking for an example on how to reuse some code, eg.:

function RemoveTableRow(row, id) 
{
   $(row).remove();
   // id should be used for ajax call
}

And then add a 'onclick' on my anchor-tag

onclick="RemoveTableRow('user-row-1', 32);"

But somehow it is not working, even if I add it on document.ready function. Can someone cut it out for me, the best practice way for doing this?

Thanks in advance. This forum is killer!

Update

I updated the code from the help I got here. This is my current code, and I would like to know if its the best way of doing this.

function RemoveTableRow(row, id) {
            $.ajax({
             type: "POST",
             url: "Default.aspx/DeleteEmployee",
             data: "{'ID':'" + id + "'}",
             beforeSend: function() {
                  $("#" + row).animate({'backgroundColor':'#fb6c6c'},300);
             },
             success: function() {
              $("#" + row).slideUp(300,function() {
                    $("#" + row).remove();
              });
             }
        });
    }

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

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

发布评论

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

评论(2

dawn曙光 2024-07-24 20:21:34

user-row-1 是表行的 ID 吗? 如果是,则您的选择器中缺少 #

function RemoveTableRow(row, id) {
   $('#'+row).remove();
   // id should be used for ajax call
}

或不更改函数,请使用完整的选择器调用它

onclick="RemoveTableRow('#user-row-1', 32);"

is user-row-1 the ID of the table row? If yes, than you are missing # in your selector

function RemoveTableRow(row, id) {
   $('#'+row).remove();
   // id should be used for ajax call
}

or without changing the function, call it with the complete selector

onclick="RemoveTableRow('#user-row-1', 32);"
那伤。 2024-07-24 20:21:34

您想将 onclick 绑定到哪里? 假设它是一个 id 为 myid 的 DOM 元素,那么这样做:

("#myid").click(function () {RemoveTableRow('user-row-1', 32);} );

Where do you want to bind the onclick to? Assuming it is a DOM element with id myid, then this will do:

("#myid").click(function () {RemoveTableRow('user-row-1', 32);} );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文