1

I have a JQuery UI modal dialog box in the form of an HTML (well ...) form with select and input (checkbox) controls. The dialog box and the controls work as one would expect, except that the controls retain their new states (e.g. checkbox selected) even if I close the dialog boxes window (or press a button "Cancel").

Is there any mechanism whereby I can make a jQuery UI dialog box retain the old control states when a certain exit button (e.g. "Cancel") is pressed, or do I have do roll my own state management in JavaScript (I hope not).

Here's an example of what currently happens and should not:

  1. Enter dialog box.
  2. Checkbox is off.
  3. Change checkbox from off to on.
  4. Close dialog box window.
  5. Open it again.
  6. Checkbox is on (I want it to be off, because the dialog box was previously "cancelled").

UPDATE Here is a relevant portion of my HTML, including a call to clone().

<script>
$(function() {
    var dialog = $("form.dialog").clone().dialog({
        autoOpen: false,
        height: 300,
        width: 300,
        modal: true,
        buttons: [
            {
                text: "Ok",
                click: function() {
                    $(this).submit();
                    $(this).dialog("close");
                }
            },
            {
                text: "Cancel",
                click: function() {
                    $(this).dialog("close");
                }
            }
        ]
    });

    $(“#dialog-button”).on("click", function() {
        dialog.dialog("open");
        return false;
    });
});

</script>

<form hidden method="post" action="" class="dialog" class="ui-dialog-titlebar ui-widget-header" title=“Test”>

        <select name="name">

            </option>
                <option value="value">
                Test
                </option>
Drux
  • 11,992
  • 13
  • 66
  • 116

2 Answers2

1

What i might do in this case is to clone the form before creating the dialog. Then, create the dialog with the cloned element. This way, the controls should be unconnected.

$('form.myForm').clone().dialog();

Note that they should not have IDs, as that would end up with duplicated IDs in a document.

Scimonster
  • 32,893
  • 9
  • 77
  • 89
  • +1 Will try ... hint re ids esp. helpful. I guess you don't have to do anything special about getting rid of the the cloned dialog box later on, right? – Drux Oct 30 '14 at 17:22
  • 1
    Well, if you don't want a bunch of unused dialogs clogging up the DOM, you can remove them when they're closed. – Scimonster Oct 30 '14 at 18:05
  • I've now tried it with clone(), but that does not seem to make a difference. I've also included the relevant portion of HTML in the updated question. Can you spot some other problems there? – Drux Nov 02 '14 at 05:55
1

You can reset the form using the reset() method of native <form> element as shown below:

$(function() {
  var dialog = $("form.dialog").dialog({
    autoOpen: false,
    height: 200,
    width: 300,
    modal: true,
    buttons: [{
      text: "Ok",
      click: function() {
        $(this).submit();
        $(this).dialog("close");
      }
    }, {
      text: "Cancel",
      click: function() {
        $(this).dialog("close").get(0).reset(); //reset the form
      }
    }],
    close: function() {
      this.reset(); //reset the form
    }
  });

  $("#dialog-button").on("click", function() {
    $("form.dialog").dialog("open");
    return false;
  });
});
<link href="http://code.jquery.com/ui/1.9.2/themes/base/jquery-ui.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="http://code.jquery.com/ui/1.9.2/jquery-ui.js"></script>
<form method="post" action="" class="dialog" class="ui-dialog-titlebar ui-widget-header" title="Test">
  <select name="name">
    <option value="value">Test</option>
    <option value="value">Option</option>
  </select>
  <input type="checkbox" />
</form>
<button id="dialog-button">Open</button>

The reset() method belongs to DOM Element. Inside the callback functions this refer to the native DOM element.

dialog() is a jQuery function. So for closing the dialog we create a jquery object by wrapping this inside $() and call $(this).dialog("close");.

Now, the jquery object $(this) doesn't have the reset() method, So we extract the DOM element from it using jQuery get() method and call it's reset() like

 $(this).dialog("close").get(0).reset();

This is equivalent to:

$(this).dialog("close"); 
this.reset();

(In short I did that to reduce the number of lines by chaining)

Inside the close callback, Since we don't have to close the dialogue manually by calling jquery method, We don't need a jquery object - we simply call this.reset() for resetting.

Reference:

Side note: The quotes () you're using is invalid in javascript.

T J
  • 42,762
  • 13
  • 83
  • 138
  • +1 Excellent and definitely the right way to go. Can you please explain a bit more about `$(this).dialog("close").get(0).reset()` vs. `this.reset()` (i.e. `$(this)` vs. `this` as applied here). What's wrong about the quotes? So two more sentences please. – Drux Nov 01 '14 at 18:03
  • 1
    @Drux Hi, I added why I used `$(this)` and `this` in two places... The quotes will throw error `Uncaught SyntaxError: Unexpected token ILLEGAL`... it's not a legal js token... see [this answer](http://stackoverflow.com/a/14663643/2333214) for more info. – T J Nov 02 '14 at 04:42
  • Thx. Those illegal quotes must be awkward copy-paste effects. I do not receive such ERRORs in my code. Will try to edit the question accordingly. – Drux Nov 02 '14 at 06:10