2012-08-08 49 views
0

我想使用jQuery UI dialog來顯示不同輸入中的錯誤。我的問題是我想定義對話框的功能只有一個,但將其與包含一個id和一個特定的錯誤消息的幾個HTML關聯。如何使用相同的jQuery UI對話框?

我該怎麼做?

目前,我有這樣的:

// definition of the dialog, to do only once 
$("#dialog").dialog({ 
    autoOpen: false, 
    show: {effect: 'fade', speed: 100}, 
    hide: {effect: 'fade', speed: 100}, 
    modal: true, 
    resizable: false, 
    title: "Incorrect value", 
    buttons: { Ok: function() { $(this).dialog("close"); }}, 
    beforeClose: function() { $("#time_at_address_years1").focus().val('') }, 
}); 

// the event, one per input 
$("#time_at_address_years1").blur(function() { 
    $("#dialog1").dialog("open"); 
    return false; 
}); 

而且一個消息的HTML是這樣的:

<div id="dialog"> 
<p>The error message here.</p> 
</div> 

回答

0

只是扭曲該功能的當前的jQuery的功能,並通過對話-selector。

function my_dialog(selector, tofocus) { 
$(selector).dialog({autoOpen:true, ...more options...}); 
} 

比這樣稱呼它:

$("#time_at_address_years1").blur(function() { 
my_dialog('#dialog1', '#time_at_address_years1'); 
return false; 
}); 

此外,你應該在jQuery的驗證器,插件http://docs.jquery.com/Plugins/Validation

+0

你是一個天才看看!不能相信它就是這麼簡單......簡單是最終的成熟。 我剛剛添加此行到您的函數,以便有效地打開框: \t $(selector).dialog(「open」); – 2012-08-08 11:26:49

+0

不客氣。 – naden 2012-08-08 12:48:53

相關問題