2013-07-27 196 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(); 
}