简体   繁体   中英

How to use the same jQuery UI dialog box?

I want to use a jQuery UI dialog to display the errors in the different inputs. My problem is that I would like to define the feature of dialog box only one but associate it with several html containing an id and a specific error message.

How could I do that?

At the moment, I have this:

// 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;
});

And the HTML of one message is like this:

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

Just warp the dialog in a function and pass that function the current jQuery-selector.

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

Than call it like this:

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

Also, you should have a look at the jQuery-Validator-Plugin http://docs.jquery.com/Plugins/Validation

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM