我有一个按下按钮时会调用的javascript函数。该函数使用ajax调用来调用另一个函数。如果/当此ajax成功完成时,我希望更改按钮的类。
$(".followUser").click(function(){
...
create_friendship(that.userId, that.friendId);
...
}
function create_friendship(user_id, friend_id){
$.ajax({
type: "POST",
...
success: function(data, textStatus, jqXHR){
variableForButtonHere.html("Request sent").removeClass("btn-default").addClass('btn-info');
到目前为止,用$(this)替换variableForButtonHere无效。我已经放了
var mydata = $(this).data();
window.alert(mydata.userId);
在两个函数中,在第一个函数中都会打印,在第二个函数中会打印 undefined
我假设必须以某种方式将$(this)传递给第二个函数。我该怎么做呢?
您可以像这样很容易地做到这一点:
$(".followUser").click(function(){
...
create_friendship($(this), that.userId, that.friendId);
...
}
function create_friendship(button, user_id, friend_id){
$.ajax({
type: "POST",
...
success: function(data, textStatus, jqXHR){
button.html("Request sent").removeClass("btn-default").addClass('btn-info');
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句