1

I need to create a pop-up window like this one:
http://gyazo.com/48a138b2e40fda7e5e72acd1b653a518 in JavaScript.

When the administrator clicks on Delete link one parameter should be passed to JavaScript on-click function.

How can I bind different actions to OK and Cancel buttons ? My actions are like this one below:

<a href="<c:url value="/Invalidate.do?val=INVALIDATE"/>">Logout</a>

Could anyone help me writing this piece of code (complete on-click function)? It would be great if you also show me how to attach this on-click to my link.

Thanks in advance

4

1 回答 1

0

您可以将window.confirm用于简单的事情。

如果您不介意所有的 javascript 阻塞并且无法控制样式,它就可以工作。

confirm并且alert内置于所有浏览器中,但它们非常有限,通常对于一次性以外的任何事情都不是一个非常好的主意。

为了获得更多控制,您必须从引导程序中引入诸如jquery ui 对话框或引导箱之类的东西,这只是 html,因此更加灵活。但除非您已经在使用相关库,否则设置起来也比较麻烦。此外,所有这些都不会阻止 javascript 的执行,这同样是更强大和更好的“实践”,但对于人们 - 尤其是初学者 - 来说也更难理解。

所以你可以做类似的事情(假设 jQuery):

$('button[name=delete]').click(function() {
  if(window.confirm("You really sure?"))
     doDelete();
});

或使用 jQuery 用户界面:

$('button[name=delete]').click(function() {
    $('<div>').text("You really sure?")  //Create a simple text element to be dialog'ed
        .dialog({
            buttons: {
                "Yes": function(){
                    doDelete()
                    $(this).dialog('close');
                }
                ,"No": function() { $(this).dialog('close'); }
        });
});      

为了完整起见,这是一种删除冗余代码的巧妙方法:

$('button[name=delete]').click(function() {
    $('<div>').text("You really sure?")  //Create a simple text element to be dialog'ed
        .dialog({
            buttons: {
                "Yes": closeAnd(doDelete)
                ,"No": closeAnd()
        });
    function closeAnd(fn) {   //this function is automatically hoisted
        return function() {   //return a handler
             $(this).dialog('close');  //value of 'this' is determined by who invokes it               
             fn && fn();   //invoke fn if it was passed
        }
    }
});      
于 2013-02-19T19:16:55.973 回答