Yii ajaxbutton:如何在成功回调函数中获取 $(this)?
Yii ajaxbutton : How to get $(this) in success callback function?
CHtml::ajaxButton('vote',Yii::app()->createUrl('land/Ajax'),
array(
'type'=>'POST',
'data'=> 'js:{"id": '.$post->id.'}',
'success'=>'js:function(e){
console.log($(this)); // I want to control this button's silblings
$("input.ajaxsubmit").prop("disabled", true);
}',
'error'=>'js:function(e){ console.log(e); }',
),
array('class'=>'btn btn-large ajaxsubmit'));
我无法在 success 函数中获取按钮本身,因为它指向 xhr 对象。
如何将按钮传递给成功功能?
CHtml::ajaxButton没有其他选择。
即使您使用普通按钮而不是 yii ajaxButton,您也无法在成功事件中访问按钮本身。我认为解决此问题的一种解决方法是 为按钮分配一个 id 然后使用它的 id 访问按钮的兄弟姐妹:
CHtml::ajaxButton('vote',Yii::app()->createUrl('land/Ajax'),
array(
'type'=>'POST',
'data'=> 'js:{"id": '.$post->id.'}',
'success'=>'js:function(e){
//You can access to button with $("#voteButton")
}',
'error'=>'js:function(e){ console.log(e); }',
),
array('class'=>'btn btn-large ajaxsubmit', 'id'=>'voteButton'));
CHtml::ajaxButton('vote',Yii::app()->createUrl('land/Ajax'),
array(
'type'=>'POST',
'data'=> 'js:{"id": '.$post->id.'}',
'success'=>'js:function(e){
console.log($(this)); // I want to control this button's silblings
$("input.ajaxsubmit").prop("disabled", true);
}',
'error'=>'js:function(e){ console.log(e); }',
),
array('class'=>'btn btn-large ajaxsubmit'));
我无法在 success 函数中获取按钮本身,因为它指向 xhr 对象。 如何将按钮传递给成功功能? CHtml::ajaxButton没有其他选择。
即使您使用普通按钮而不是 yii ajaxButton,您也无法在成功事件中访问按钮本身。我认为解决此问题的一种解决方法是 为按钮分配一个 id 然后使用它的 id 访问按钮的兄弟姐妹:
CHtml::ajaxButton('vote',Yii::app()->createUrl('land/Ajax'),
array(
'type'=>'POST',
'data'=> 'js:{"id": '.$post->id.'}',
'success'=>'js:function(e){
//You can access to button with $("#voteButton")
}',
'error'=>'js:function(e){ console.log(e); }',
),
array('class'=>'btn btn-large ajaxsubmit', 'id'=>'voteButton'));