如何将$(此)变量传递到另一个函数javascript
我有一个javascript函数,当按下按钮时会调用它。此函数使用ajax调用调用另一个函数。如果/当这个ajax成功完成时,我希望按下按钮的类发生变化如何将$(此)变量传递到另一个函数javascript,javascript,jquery,Javascript,Jquery,我有一个javascript函数,当按下按钮时会调用它。此函数使用ajax调用调用另一个函数。如果/当这个ajax成功完成时,我希望按下按钮的类发生变化 $(".followUser").click(function(){ ... create_friendship(that.userId, that.friendId); ... } function create_friendship(user_id, friend_id){ $.ajax({ type:
$(".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');
到目前为止,将variableForButtonHere替换为$(this)尚未奏效。我把
var mydata = $(this).data();
window.alert(mydata.userId);
在两个函数中以及在第一个函数中打印,在第二个函数中,如果打印未定义
我假设$(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');
$(".followUser").click(function(){
...
create_friendship(that.userId, that.friendId, this);
...
}
function create_friendship(user_id, friend_id, setThis){
$.ajax({
type: "POST",
context: setThis, // <=== HERE ===
...
success: function(data, textStatus, jqXHR){
// === Now, `this` will refer to your button element!
$(this).html("Request sent").removeClass("btn-default").addClass('btn-info');
您可以很容易地这样做:
$(".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');
$(".followUser").click(function(){
...
create_friendship(that.userId, that.friendId, this);
...
}
function create_friendship(user_id, friend_id, setThis){
$.ajax({
type: "POST",
context: setThis, // <=== HERE ===
...
success: function(data, textStatus, jqXHR){
// === Now, `this` will refer to your button element!
$(this).html("Request sent").removeClass("btn-default").addClass('btn-info');
选项1:在$.ajax
调用中设置上下文
$。ajax
有一个选项,允许您在回调函数中设置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');
$(".followUser").click(function(){
...
create_friendship(that.userId, that.friendId, this);
...
}
function create_friendship(user_id, friend_id, setThis){
$.ajax({
type: "POST",
context: setThis, // <=== HERE ===
...
success: function(data, textStatus, jqXHR){
// === Now, `this` will refer to your button element!
$(this).html("Request sent").removeClass("btn-default").addClass('btn-info');
选项1:在$.ajax
调用中设置上下文
$。ajax
有一个选项,允许您在回调函数中设置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');
$(".followUser").click(function(){
...
create_friendship(that.userId, that.friendId, this);
...
}
function create_friendship(user_id, friend_id, setThis){
$.ajax({
type: "POST",
context: setThis, // <=== HERE ===
...
success: function(data, textStatus, jqXHR){
// === Now, `this` will refer to your button element!
$(this).html("Request sent").removeClass("btn-default").addClass('btn-info');
除了通过参数之外,没有其他方法可以传递<代码>$(this)。除了通过参数,也没有办法传递
这个
。@LuisMasuelli我也这么认为,但后来了解到这个
的值取决于函数的调用方式。因此,您可以根据调用函数的方式设置this
的值。例如,jQuery.proxy
函数如何使用它来更改此
@RobinMailfait的值。我的意思是其他。我已经更新了我的答案。除了通过参数,没有其他方法可以传递$(this)
。除了通过参数,也没有办法传递这个
。@LuisMasuelli我也这么认为,但后来了解到这个
的值取决于函数的调用方式。因此,您可以根据调用函数的方式设置this
的值。例如,jQuery.proxy
函数如何使用它来更改此
@RobinMailfait的值。我的意思是其他。我已经更新了我的答案。你是对的,我会在SO允许的时候接受这个答案。你是对的,我会在SO允许的时候接受这个答案。