2013-07-27 195 views
2

我的警报已到达并可以使用,但close方法不起作用。任何想法为什么?jQuery对话框不关闭

我的javascript:

var myDialog = $('#divContactUs'); 

    myDialog.dialog({ 
       autoOpen: false, 
       title: "Send Us A Note:", 
       modal: true, 
       buttons: [{ 
        id: "btn-send", 
        text: "Send", 
        click: function() { 
         // you can now use the reference to the dialog 
         myDialog.dialog("close"); 
         alert('test'); 
        } 
       }] 

      }); 
     }); 

而且我的html:

<div id="divContactUs" style="display:none"> 
Name: &nbsp <input type="text" name="Name" /><br /> 
Email: &nbsp <input type="text" name="Email" /><br /> 
Phone: &nbsp <input type="text" name="Phone" /><br /> 
Message:<br /> 
<textarea name="Message"></textarea> 
</div> 

回答

2

在按钮的范围单击$(this)指按钮,而不是模式。我会先将模态选择器设置为一个变量,然后再引用它;

var myDialog = $('#divContactUs'); 

myDialog.dialog({ 
    autoOpen: false, 
    title: "Send Us A Note:", 
    modal: true, 
    buttons: [{ 
     id:"btn-send", 
     text: "Send", 
     click: function() { 
      // you can now use the reference to the dialog 
      myDialog.dialog("close") 
     } 
    } 
}); 
+0

嗯。我认为这会做到这一点,但我仍然有同样的问题。任何其他想法? –

+0

@BillGreer我可以看到你的代码吗? – Jivings

+0

我在过去的一个小时左右多次修改过它....我在上面添加了我最近的代码。 –

0

您的脚本正在工作。不要忘了添加分号;添加行尾$(this).dialog("close");

这里有一个的jsfiddle证明它JsFiddle

+1

[分号是可选的](http://mislav.uniqpath.com/2010/05/semicolons/) – Jivings

+0

你学到了一些东西,但我会保留我的分号“只是为了安全”。 – L105

0

有时IE会引发错误“未指定的错误”。

try{ 
    $(selector).dialog("close");. 
}catch(e){ 
    $(selector).parent().hide(); 
}