Javascript 如何使用相同的jQuery UI对话框?

Javascript 如何使用相同的jQuery UI对话框?,javascript,jquery-ui,modal-dialog,Javascript,Jquery Ui,Modal Dialog,我想用a来显示不同输入中的错误。我的问题是,我只想定义一个对话框的功能,但要将它与几个包含id和特定错误消息的html关联起来 我怎么能这么做 目前,我有以下几点: // definition of the dialog, to do only once $( "#dialog" ).dialog({ autoOpen: false, show: {effect: 'fade', speed: 100}, hide: {effect: 'fade', speed: 100

我想用a来显示不同输入中的错误。我的问题是,我只想定义一个对话框的功能,但要将它与几个包含id和特定错误消息的html关联起来

我怎么能这么做

目前,我有以下几点:

// 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>
$( "#time_at_address_years1" ).blur(function() {
 my_dialog('#dialog1', '#time_at_address_years1');
 return false;
});

此处显示错误消息


只需在函数中扭曲对话框,并将当前jQuery选择器传递给该函数即可

function my_dialog(selector, tofocus) {
 $(selector).dialog({autoOpen:true, ...more options...});
}
而不是这样称呼它:

<div id="dialog">
<p>The error message here.</p>
</div>
$( "#time_at_address_years1" ).blur(function() {
 my_dialog('#dialog1', '#time_at_address_years1');
 return false;
});

另外,您还应该看看jQuery验证程序插件

您真是个天才!真不敢相信事情就这么简单。。。简单是最复杂的。我刚刚在函数中添加了这一行,以便有效地打开框:$(选择器).dialog(“打开”);