2011-11-12 2 views
1
function pageLoad() { 


     $(".VoteUp").live("click", function() { 
      var Id = $(this).attr("index"); 
      d = JSON.stringify({ "Id": Id }) 
      $.ajax({ 
       type: 'POST', 
       url: '../API/Services.asmx/LikeId', 
       data: d, 
       contentType: 'application/json; charset=utf-8', 
       dataType: 'json', 
       success: testFunc, 
       error: errFunc 
      }); 

      $(".VoteUp").each(function() { 
       $(this).attr("disabled", "disabled"); 
      }); 

     }); 

     function testFunc(result, userContext, methodName) { 
      if (result.d[0] == "true") { 
       TheVoteUpControl.val("added"); 
      } 
      else { 
       TheVoteUpControl.val("deleted"); 
      } 
      $(".VoteUp").each(function() { 
       $(this).removeAttr("disabled"); 
      }); 

     } 
     function errFunc() { 
      alert('error'); 
     } 

成功した場合、同じ.VoteUpコントロールの値を変更するにはどうすればよいですか?イベントを開始した同じコントロールをどのようにターゲットにするのか分かりません。 $( "。VoteUp")ポストバック時にjQuery AJAX POSTを開始した同じコントロールをターゲットにする

提案はありますか?

答えて

1

私が考えることができる唯一の方法は、グローバル変数の要素をキャッシュし、成功のコールバックでそれをキャッチすることです。また、私は有効とそれぞれ

$(".VoteUp").live("click", function (e) { 
     e.preventDefault(); 
     $this=$(this); <-- cache this in the global variable 
     var Id = $(this).attr("index"); 
     d = JSON.stringify({ "Id": Id }) 
     $.ajax({ 
      beforeSend:function(){ 
       $(".VoteUp").attr("disabled", "disabled"); 
      }, 
      type: 'POST', 
      url: '../API/Services.asmx/LikeId', 
      data: d, 
      contentType: 'application/json; charset=utf-8', 
      dataType: 'json', 
      success: testFunc, 
      error: errFunc 
      complete:function(){ 
        $(".VoteUp").removeAttr("disabled"); 
      } 
     }); 
    }); 


    function testFunc(result, userContext, methodName) { 
     if (result.d[0] == "true") { 
      $this.val("added"); 
     } 
     else { 
      $this.val("deleted"); 
     } 


    } 
+0

completebeforeSendコールバックで$(".VoteUp")の無効化を移動してきたねえ、答えてくれてありがとう。これはまだ動作しておらず、VoteUpコントロールの値を変更していません。 – user1027620

+1

投票アップコントロールとは何ですか? – Rafay

+0

スパンです。 .val()を.html()に変更すると、あなたのメソッドが動作します。ありがとう。 – user1027620

関連する問題