2011-10-07 35 views
2

我有一點dillema :)如何跟蹤由jQuery對話框調用的動作?

我有一個鏈接供用戶投票的項目。點擊鏈接會生成一個jQuery AJAX調用,以檢查該用戶是否已登錄。如果沒有,則該對話框顯示一個要登錄的表單。

但問題是,jQuery調用登錄和彈出框的整個位在不同的地方。

我需要做的是檢查用戶是否成功登錄,並更新投票計數。

我做這個網站:http://www.problemio.com

這裏是我的jQuery代碼至今:

<script type="text/javascript"> 
$(document).ready(function() 
{ 
    var $dialog = $('#loginpopup') 
     .dialog({ 
     autoOpen: false, 
     title: 'Login Dialog' 
     }); 

     $("#newprofile").click(function() { 
      $("#login_div").hide(); 
      $("#newprofileform").show(); 
     }); 


    $('.vote_up').click(function() 
    {   
     problem_id = $(this).attr("data-problem_id"); 

     var dataString = 'problem_id='+ problem_id + '&vote=+'; 

     $.ajax({ 
       type: "POST", 
       url: "/problems/vote.php", 
       dataType: "json", 
       data: dataString, 
       success: function(data) 
       {   
        // ? :) 
        alert (data); 
       }, 
       error : function(data) 
       { 
        errorMessage = data.responseText; 

        if (errorMessage == "not_logged_in") 
        { 
         // Try to create the popup that asks user to log in. 
         $dialog.dialog('open'); 

         // prevent the default action, e.g., following a link 
         return false; 
        } 
        else 
        { 
         alert ("not"); 
        } 

        //alert(JSON.stringify(data)); 
       } 
      }); 


     //Return false to prevent page navigation 
     return false; 
    }); 

    $('.vote_down').click(function() 
    { 
     alert("down"); 

     problem_id = $(this).attr("data-problem_id"); 

     var dataString = 'problem_id='+ problem_id + '&vote=-';   

     //Return false to prevent page navigation 
     return false; 
    });  
}); 
</script> 

它除了行$dialog.dialog('open');之後所有的作品 - 我不知道該怎麼

  1. 獲取失敗成功的信號,並且不確切知道如何
  2. 更新th因爲它只是可以在頁面上進行投票的許多項目之一,所以這個項目被投票通過。

我該怎麼做這兩件事?

回答

1

試試這個辦法:

  1. div這是你的登錄對話框中的隱藏輸入。
  2. 設置與您以前problem_id.dialog('open')
  3. Login按鈕點擊的成功回調,檢索隱藏輸入problem_id並進行表決時或投降。

希望幫助

編輯:(試圖OP的第二個意見後編寫一個可行的例子)

<script type="text/javascript"> 
    $(document).ready(function() { 
     var $dialog = $('#loginpopup') 
       .dialog({ 
        autoOpen: false, 
        title: 'Login Dialog' 
       }); 

     var $problemId = $('#theProblemId', '#loginpopup'); 

     $("#newprofile").click(function() { 
      $("#login_div").hide(); 
      $("#newprofileform").show(); 
     }); 


     $('.vote_up').click(function() { 
      var problem_id = $(this).attr("data-problem_id"); 
      voteUp(problem_id); 
      //Return false to prevent page navigation 
      return false; 
     }); 

     var voteUp = function(problem_id) { 
      var dataString = 'problem_id=' + problem_id + '&vote=+'; 

      $.ajax({ 
       type: "POST", 
       url: "/problems/vote.php", 
       dataType: "json", 
       data: dataString, 
       success: function(data) { 
        // ? :) 
        alert(data); 
       }, 
       error : function(data) { 
        errorMessage = data.responseText; 
        if (errorMessage == "not_logged_in") { 
         //set the current problem id to the one within the dialog 
         $problemId.val(problem_id); 

         // Try to create the popup that asks user to log in. 
         $dialog.dialog('open'); 

         // prevent the default action, e.g., following a link 
         return false; 
        } 
        else { 
         alert("not"); 
        } 

        //alert(JSON.stringify(data)); 
       } 
      }); 
     }; 

     $('.vote_down').click(function() { 
      alert("down"); 

      problem_id = $(this).attr("data-problem_id"); 

      var dataString = 'problem_id=' + problem_id + '&vote=-'; 

      //Return false to prevent page navigation 
      return false; 
     }); 

     $('#loginButton', '#loginpopup').click(function() { 
      $.ajax({ 
       url:'url to do the login', 
       success:function() { 
        //now call cote up 
        voteUp($problemId.val()); 
       } 
      }); 
     }); 
    }); 
</script> 
+0

謝謝...現在想.. – GeekedOut

+0

我覺得這是一個inherint問題這種方法,我不能設置隱藏字段與該problem_id,直到用戶點擊該特定問題。頁面上有很多問題,系統不知道在點擊之前要使用哪個problem_id。那有意義嗎?考慮到這個限制,是否還有辦法讓這項工作成爲可能? – GeekedOut

+0

Witha很少重構,應該是可以做到的,我仍然認爲,即使有你提到的限制。我試圖通過添加我建議的解決方案來增強您提供的代碼示例。看看這是否有幫助... –