2011-04-12 46 views
2

即时通讯使用以下代码作为javascript中的确认框的替代品。如何让fancybox通过href onclick显示“yes/no confirm box”?

function fancyAlert(msg) { 
    jQuery.fancybox({ 
     'modal' : true, 
     'content' : "<div style=\"margin:1px;width:240px;\">"+msg+"<div style=\"text-align:right;margin-top:10px;\"><input style=\"margin:3px;padding:0px;\" type=\"button\" onclick=\"jQuery.fancybox.close();\" value=\"Ok\"></div></div>"  }); } 

function fancyConfirm(msg, callback) { 
    var ret; 
    jQuery.fancybox({ 
     'modal' : true, 
     'content' : "<div style=\"margin:1px;width:240px;\">"+msg+"<div style=\"text-align:right;margin-top:10px;\"><input id=\"fancyConfirm_cancel\" style=\"margin:3px;padding:0px;\" type=\"button\" value=\"Cancel\"><input id=\"fancyConfirm_ok\" style=\"margin:3px;padding:0px;\" type=\"button\" value=\"Ok\"></div></div>", 
     onComplete : function() { 
      jQuery("#fancyConfirm_cancel").click(function() { 
       ret = false; 
       jQuery.fancybox.close(); 
      }) 
      jQuery("#fancyConfirm_ok").click(function() { 
       ret = true; 
       jQuery.fancybox.close(); 
      }) 
     }, 
     onClosed : function() { 
      if (typeof callback == 'function'){ callback.call(this, ret); }; 
     } 
    }); 
} 

function fancyConfirm_text() { 
    fancyConfirm("Ceci est un test", function(ret) { 
     alert(ret) 
    }) 
} 
使用这样的超级链接

和IM:

<a href="http://www.example.com/" onclick="fancyConfirm("Are you sure you want to delete?");">Test</a> 

IM卡,它只是不会显示任何内容?我认为它有事情做与fancyConfirm所需的回调变量,但即时通讯无法确定是什么这意味着它是如何应用的。

感谢您的帮助。

回答

2

链接属性存在错误。您不能将双引号放在双引号内而不能转义。现在,你的onclick属性就是: “fancyConfirm(”

尝试:

<a href="http://www.example.com/" onclick="fancyConfirm('Are you sure you want to delete?');">Test</a> 

或者:

<a href="http://www.example.com/" onclick="fancyConfirm(\"Are you sure you want to delete?\");">Test</a> 
+0

对不起,这是我的错误,我已经有单引号,但它仍然不起作用? ,还有其他建议吗? – PHPNewEE 2011-04-19 14:02:13

0

你需要这个来代替:

<a href="#" onclick="fancyConfirm('Are you sure you want to delete?');return false;">Test</a> 

这应该工作哈希(#)不会显示在URL中,因为return false;只会使onclick发生什么。

另外,我知道这是显而易见的,但你有Fancybox和jQuery在页面上定义?