2010-10-11 172 views
0
function manageVoting() { 
    var parameter; 
    var myVoting; 
    var divVoting; 
    var divVotes; 
    var value = -1; 
    var parameterData; 
    $('div.votemaincontainer').each(function() { 
     parameter = $(this).find('#[id$= hfUrl]').val(); 
     myVoting = parseInt($(this).find('#[id$=hfMyVote]').val()); 
     divVoting = $(this).find('[id$=divVoting]'); 
     divVotes = $(this).find('[id$=divVotes]'); 


     $('img.voteupImage').live('click', function() { 
      if (myVoting == 1) { 
       alert(" you have already voted"); 
      } 
      else { 
       value = 1; 
      } 
     }); 
     $('img.votedownImage').live('click', function() { 
      if (myVoting == 0) { 
       alert(" you have already voted"); 
      } 
      else { 
       value = 0; 
      } 
     }); 


     if (value == 0 || value == 1) 
     { 
      parameterData = parameter + value + "'}"; 

      $.ajax({ 
       type: 'POST', 
       url: 'UserControls/Vote/VoteAction.aspx/Voting', 
       data: parameterData, 
       contentType: "application/json; charset=utf-8", 
       dataType: "json", 
       success: function(data) { 
        var result = eval(data.d); 

        if (result) { 
         if (result.length > 1) { 
          alert("i am inside result here result length >1"); 
          if (result[1] == 1 && result[2] == 1) 
          { 
           $('img.voteupImage').attr('src', 'UserControls/Vote/Images/aftervote_arrow_up.png'); 
           $('img.votedownImage').attr('src', 'UserControls/Vote/Images/arrow_down.png'); 
           $('div.divVotes').html(result[0]); 
          } 
          else 
          { 
           alert('U can not vote more than 1'); 
          } 
          $('#[id$=hfMyVote]').html(result[1]); 
          if (result[1] == 0 && result[2] == 1) 
          { 
           $('img.voteupImage').attr('src', 'UserControls/Vote/Images/Arrow_Up.png'); 
           $('img.votedownImage').attr('src', 'UserControls/Vote/Images/aftervote_down.png'); 
           $('div.divVotes').html(result[0]); 
          } 
          else 
          { 
           alert('U can not vote down more than 1'); 
          } 
         } 
         else { 
          $('div.divVotes').html(result[0] - 1); 
          alertDialog("Rating any knowledge item is only available for Registered User.<br>Do you want to <a class='signUpPopUp' href='signup.aspx'> signup</a> Now?"); 
         } 

        } 
       }, 
       error: function() { 
        alert("i am inside error"); 

       } 
      }); 
     } 
    }); 
} 



$(function() { 
    manageVoting(); 
}); 

我已經寫了上面的代碼,但當我在圖像上覆制,然後$ .ajax不執行代碼中的問題。ajax函數調用錯誤

回答

1

click現場處理程序中,您只設置了value參數的值。之後你就不會執行其餘的代碼。嘗試是這樣的(未經測試):

function manageVoting() { 
    var parameter; 
    var myVoting; 
    var divVoting; 
    var divVotes; 
    var value = -1; 
    var parameterData; 
    $('div.votemaincontainer').each(function() { 
     parameter = $(this).find('#[id$= hfUrl]').val(); 
     myVoting = parseInt($(this).find('#[id$=hfMyVote]').val()); 
     divVoting = $(this).find('[id$=divVoting]'); 
     divVotes = $(this).find('[id$=divVotes]'); 

     function processVote(value) { 
      if (value == 0 || value == 1) { 
       parameterData = parameter + value + "'}"; 

       $.ajax({ 
        type: 'POST', 
        url: 'UserControls/Vote/VoteAction.aspx/Voting', 
        data: parameterData, 
        contentType: "application/json; charset=utf-8", 
        dataType: "json", 
        success: function(data) { 
         var result = eval(data.d); 

         if (result) { 
          if (result.length > 1) { 
           alert("i am inside result here result length >1"); 
           if (result[1] == 1 && result[2] == 1) 
           { 
            $('img.voteupImage').attr('src', 'UserControls/Vote/Images/aftervote_arrow_up.png'); 
            $('img.votedownImage').attr('src', 'UserControls/Vote/Images/arrow_down.png'); 
            $('div.divVotes').html(result[0]); 
           } 
           else 
           { 
            alert('U can not vote more than 1'); 
           } 
           $('#[id$=hfMyVote]').html(result[1]); 
           if (result[1] == 0 && result[2] == 1) 
           { 
            $('img.voteupImage').attr('src', 'UserControls/Vote/Images/Arrow_Up.png'); 
            $('img.votedownImage').attr('src', 'UserControls/Vote/Images/aftervote_down.png'); 
            $('div.divVotes').html(result[0]); 
           } 
           else 
           { 
            alert('U can not vote down more than 1'); 
           } 
          } 
          else { 
           $('div.divVotes').html(result[0] - 1); 
           alertDialog("Rating any knowledge item is only available for Registered User.<br>Do you want to <a class='signUpPopUp' href='signup.aspx'> signup</a> Now?"); 
          } 

         } 
        }, 
        error: function() { 
         alert("i am inside error"); 

        } 
       }); 
      } 
     } 

     $('img.voteupImage').live('click', function() { 
      if (myVoting == 1) { 
       alert(" you have already voted"); 
      } 
      else { 
       value = 1; 
       processVote(value); 
      } 
     }); 
     $('img.votedownImage').live('click', function() { 
      if (myVoting == 0) { 
       alert(" you have already voted"); 
      } 
      else { 
       value = 0; 
       processVote(value); 
      } 
     }); 
    }); 
} 

$(function() { 
    manageVoting(); 
}); 

此外,當您使用的是JSON數據類型與$.ajax,你不應該需要eval該回來的數據。 jQuery已經爲你評估JSON,並返回一個本地JavaScript對象。