2012-01-20 62 views
0

我有一个对话框,它有两个按钮,一个继续,一个取消。为什么我的对话框只能工作一次?

$(document).ready(function(){ 
$("[name=resetPwd]").click(function(){ 
    var id = $(this).attr("userid"); 
    var email = $("[field=emailAddress].[userid="+id+"]").text(); 
    var name = $("[field=firstName].[userid="+id+"]").text(); 

    var message = $('<div></div>') 
    .html('You are about to reset the password for:<br/>'+name+'.<br/>This will send an e-mail to:<br/>'+ 
      email+'<br/><br/><input id="_continue" type="button" value="Continue" /> <input id="_cancel" type="button" value="Cancel" />') 
    .dialog({ 
     title: 'Reset Password' 
    }); 

    message.dialog('open'); 


    $("#_cancel").click(function(){ 
     message.dialog('close'); 
    }); 

}); 

它工作得很好,(第一次)我所有的数据是存在的,当我点击取消,它关闭,但如果我尝试再次取消犯规了做任何事情。有谁知道我做错了什么?请致电

回答

0

您每次单击resetPwd元素时都会创建一个新的对话框对象。

您应该在点击处理程序外部创建对话框,并使用打开和关闭方法引用它。

+0

当我在点击处理程序之外创建对话框时,它会在每个页面上自动打开。有没有办法让它只能用于这一页即时工作? – Dagron

+0

谢谢你一直工作 – Dagron

+0

不客气。真高兴你做到了。 – BZink

1

尝试将$("#_cancel").click移出$("[name=resetPwd]").click正文函数。此外,您没有正确地结束$(document).ready函数。

试试这个:

$(document).ready(function(){ 
    $("[name=resetPwd]").click(function(){ 
     var id = $(this).attr("userid"); 
     var email = $("[field=emailAddress].[userid="+id+"]").text(); 
     var name = $("[field=firstName].[userid="+id+"]").text(); 

     var message = $('#message') 
     .html('You are about to reset the password for:<br/>'+name+'.<br/>This will send an e-mail to:<br/>'+ 
       email+'<br/><br/><input id="_continue" type="button" value="Continue" /> <input id="_cancel" type="button" value="Cancel" />') 
     .dialog({ 
      title: 'Reset Password' 
     }); 

     message.dialog('open'); 
    }); 
    $("#_cancel").click(function(){ 
     $('#message').dialog('close'); 
    }); 
}); 

编辑:创建一个ID =消息的空div并尝试更新的代码。

+0

我认为,如果我已经取消点击处理程序之前,甚至是按钮,处理程序不会找到它们..这是真的吗?我认为这是因为按钮只是不工作...我应该做一个当前2答案的混合? – Dagron

相关问题