繁体   English   中英

在 jQuery 函数中传递参数

[英]pass parameter inside jQuery Function

我想将“this”传递给“success”函数我试图将“this”关键字作为参数放在成功函数的括号内..如何在成功函数中传递这个关键字? ......但它没有用......可能是我的英语不好......抱歉。

<script>
        $(document).ready(function () {
            $(".join").click(
                function () {
                    if ($(this).text() == "join")
                    {
                        var FollowOptions = {};
                        @*FollowOptions.url = "/@CultureInfo.CurrentCulture.Name/Communities/Follow/";*@
                        FollowOptions.url = "/@CultureInfo.CurrentCulture.Name/Groups/Join/";
                        FollowOptions.data = { id: $(this).attr("name") };
                        FollowOptions.success = function (this) {
                            $(this).prop("text", "leave");
                            $(this).removeClass("btn btn-info");
                            $(this).addClass("btn btn-danger");

                        };
              $.ajax(FollowOptions);
                    }
                    else
                    {
                        var FollowOptions = {};
                        FollowOptions.url = "/@CultureInfo.CurrentCulture.Name/Groups/UnJoin/";
                        FollowOptions.data = { id: $(this).attr("name") };
                        FollowOptions.success = function (this) {
                            $(this).prop("text", "join");
                            $(this).removeClass("btn btn-danger");
                            $(this).addClass("btn btn-info");
                           };
                        $.ajax(FollowOptions);
                    }
                });
        });
</script>

您有 2 个选择:

  1. 使函数成为一个胖箭头函数。 它不会创建自己的“这个”。

     FollowOptions.success = ()=> { $(this).text("إلغاء الإنصمام"); $(this).removeClass("btn btn-info"); $(this).addClass("btn btn-danger"); };
  2. 将 this 绑定到普通函数

     FollowOptions.success = function () { $(this).text("إلغاء الإنصمام"); $(this).removeClass("btn btn-info"); $(this).addClass("btn btn-danger"); }.bind(this);

我推荐第一个选项,因为它更简洁('bind' 创建了一个新函数,如果你习惯使用它,你可能会在处理事件侦听器时遇到麻烦,例如,当你尝试删除事件侦听器时)。

你可以试试下面的代码:

$(document).ready(function () {
    $(".join").click(
        function () {
            var that = this;

            if ($(that).text() == "إنضمام")
            {
                var FollowOptions = {};
                @*FollowOptions.url = "/@CultureInfo.CurrentCulture.Name/Communities/Follow/";*@
                FollowOptions.url = "/@CultureInfo.CurrentCulture.Name/Groups/Join/";
                FollowOptions.data = { id: $(that).attr("name") };
                FollowOptions.success = function () {
                    $(that).text("إلغاء الإنصمام");
                    $(that).removeClass("btn btn-info");
                    $(that).addClass("btn btn-danger");

                };
                $.ajax(FollowOptions);
            }
            else
            {
                var FollowOptions = {};
                FollowOptions.url = "/@CultureInfo.CurrentCulture.Name/Groups/UnJoin/";
                FollowOptions.data = { id: $(that).attr("name") };
                FollowOptions.success = function () {
                    $(that).text("إنضمام");
                    $(that).removeClass("btn btn-danger");
                    $(that).addClass("btn btn-info");
                    };
                $.ajax(FollowOptions);
            }
        });
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM