2016-11-04 8 views
2

フォームを使用する代わりに更新関数を作成しようとしています。私はdata- *属性とajaxを使用します。値を更新した後にボタンが発射されないようにしたいのですが、私のボタンの入力(数値)とdata- *属性の値がすでに同じです。私はあなたが私を助けてくれることを願っています。 success:ハンドラでajaxとdata- *属性を使用してボタンを更新します

$('body').on('click', '.btn-edituser', function(){ 
var button = $(this); 
var upval = button.parent().prev().find('.data-avail').val();//get the value of the input(number) 
var updateId = button.data('updateid');// this is the user id to be pass on to the php file 
var tempAv = button.data('tempavail'); //doesn't get the new values after updating this is my PROBLEM 

if(upval != tempAv){ //check if the input(number) has been changes value/doesn't have the same value with the data attribute (temporary) 
    button.addClass('disabled'); 
    button.html('<i class="fa fa-refresh fa-spin"></i> Updating...'); 

    $.ajax({ 
     url:'./inc/update-avail.php', 
     type: 'POST', 
     data:{upval:upval, updateId:updateId}, 
     success:function(data){    
      button.removeClass('disabled'); 
      button.attr('data-tempavail', upval); //update the data attribute (temporary) 
      button.removeClass('btn-info').addClass('btn-success').html('<i class="fa fa-check"></i> Success'); 
      setTimeout(function(){     
       button.removeClass('btn-success').addClass('btn-info').html('<i class="fa fa-pencil"></i> Edit'); 
      }, 1000); 
     }   
    }); 
} }); 
+0

変更button.attr。 button.attr( 'data-tempavail'、tempAv) – coder

答えて

1

:)事前に感謝、属性の要素に直接1意志店として、後$.data代わりの$.attrを介して情報をデータを設定してみてください。 ように、コードは次のようにする必要があります:(upval 'データ・tempavail'、)

success:function(data){    
    button.removeClass('disabled'); 
    button.data('tempavail', upval); //update the data which can be seen via button.data('tempavail') again on next button click 
    button.removeClass('btn-info').addClass('btn-success').html('<i class="fa fa-check"></i> Success'); 
    setTimeout(function(){     
     button.removeClass('btn-success').addClass('btn-info').html('<i class="fa fa-pencil"></i> Edit'); 
    }, 1000); 
}   
+0

これは機能しました!私はそれについて考えなかった。どうもありがとう! – johnnyblade

+0

よろしくお願いします。@johnnyblade ...! – vijayP

+0

私は別の問題を抱えていますが、それはあなたが私を助けることができますか? – johnnyblade

関連する問題