2011-10-06 46 views
0

我是一个jQuery新手一个对话框弹出,我试图在错误试图创建使用jQuery

error : function(data) 

的情况下打开一个弹出的对话框中给用户我在jQuery代码是什么至今:

<script type="text/javascript"> 
$(document).ready(function() 
{ 
    var $dialog = $('<div></div>') 
      .html('This dialog will show every time!') 
      .dialog({ 
       autoOpen: false, 
       title: 'Basic Dialog' 
      }); 

    $('.vote_up').click(function() 
    {   
     alert ("test: " + $(this).attr("data-problem_id")); 
     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) 
       { 
        //alert("ajax error, json: " + data.responseText); 
        errorMessage = data.responseText; 

        if (errorMessage == "not_logged_in") 
        { 
         alert ("errr"); 

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

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

        //alert(JSON.stringify(data)); 
        //for (var i = 0, l = json.length; i < l; ++i) 
        //{ 
        // alert (json[i]); 
        //} 
       } 
      }); 


     //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> 

我得到一个JavaScript错误,对象没有方法对话框。 http://www.problemio.com

我怎样才能解决这个问题:如果你按下“投票了”链接似乎希腊到我:)

错误发生在这里?我可以让对话出现吗?这将是要求人们登录该网站或注册。

谢谢!

回答

1

您需要在页面上包含jQuery UI JavaScript and CSS files。您还需要在对话框元素附加到页面的DOM:

var $dialog = $('<div></div>') 
     .html('This dialog will show every time!') 
     .appendTo('body') 
     .dialog({ 
      autoOpen: false, 
      title: 'Basic Dialog' 
     }); 

附注:我强烈建议您更新到jQuery的较新版本。您的网站正在使用1.3(2009年1月发布); the latest version是1.6.4(2011年9月)。

+0

我会更新jQuery的版本。它是否像换出标题中的链接一样简单,以引用新的链接? – GeekedOut

+0

是和不是。升级可能会破坏当前代码中的现有功能。 –

+0

顺便说一句,在做.appendTo('身体')像你建议和其他建议在网页上做一个新的div和追加到该div有什么区别? – GeekedOut

1

不确定var $dialog = $('<div></div>')是非常合法!

我认为你需要这样做;

VAR $dialog = $('.MyDiv')

然后

<div class="MyDiv"></div> 
+0

我明白了 - 我应该在哪里将该div放在页面上?在页脚下方的最下方?它会在哪里?谢谢! – GeekedOut

+0

是的,只是把它粘在页面上的某处,因为jQuery的用户界面会稍后放置它 – griegs