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

时间:2012-08-08 08:06:11

标签: javascript jquery-ui modal-dialog

我想使用jQuery UI dialog来显示不同输入中的错误。我的问题是我想只定义一个对话框的功能,但是将它与几个包含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>

1 个答案:

答案 0 :(得分: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-Validator-Plugin http://docs.jquery.com/Plugins/Validation