在jQuery函数中传递参数
我想把这个传递给success函数我试着把这个关键字作为参数放在success函数的括号里..如何在success函数里面传递这个关键字。。。但它没有工作…可能是我的英语不好…对不起在jQuery函数中传递参数,jquery,Jquery,我想把这个传递给success函数我试着把这个关键字作为参数放在success函数的括号里..如何在success函数里面传递这个关键字。。。但它没有工作…可能是我的英语不好…对不起 <script> $(document).ready(function () { $(".join").click( function () { if ($(this).text() == "
<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>
您可以尝试以下代码: $document.readyfunction{ $.join.click 作用{ var=这个; 如果$that.text== { var FollowOptions={}; @*FollowOptions.url=//@CultureInfo.CurrentCulture.Name/Communities/Follow/*@ FollowOptions.url=//@CultureInfo.CurrentCulture.Name/Groups/Join/; FollowOptions.data={id:$that.attrname}; FollowOptions.success=函数{ $that.文本; $that.removeClassbtn btn信息; $that.addClassbtn btn危险; }; $.ajaxFollowOptions; } 其他的 { var FollowOptions={}; FollowOptions.url=//@CultureInfo.CurrentCulture.Name/Groups/UnJoin/; FollowOptions.data={id:$that.attrname}; FollowOptions.success=函数{ $that.textإنضمام; $that.removeClassbtn btn危险; $that.addClassbtn btn信息; }; $.ajaxFollowOptions; } }; }; 您有两种选择: 将函数设为胖箭头函数。它不会为此创建自己的
FollowOptions.success = ()=> {
$(this).text("إلغاء الإنصمام");
$(this).removeClass("btn btn-info");
$(this).addClass("btn btn-danger");
};
将此绑定到普通函数
FollowOptions.success = function () {
$(this).text("إلغاء الإنصمام");
$(this).removeClass("btn btn-info");
$(this).addClass("btn btn-danger");
}.bind(this);
我推荐第一个选项,因为它更干净的“绑定”创建了一个新函数,如果您习惯了使用它,在处理eventlistener时可能会遇到麻烦,例如,当您尝试删除eventlistener时。感谢您的贡献。。。但它不起作用@我刚刚编辑了代码,请再试一次,看看它是否对你有效。谢谢它起作用了。。。在第一个选项中,你们的意思是我不需要传递参数,因为我不需要使用函数…这是最简单和更高性能的选项