开发者

how to prevent a jquery ui dialog to close on hitting enter

I have my HTML defined like this:

forgot password

<div id='dialog-form-lostpass' title='password recovery' class='hidden lsdialog'>
    <p id='explain'>Please enter your username or email address. We will then immediately mail your
        password to your registered email address</p>
    <p id='progress'></p>
    <form>
        <fieldset>
            <label for='username'>username or email</label><br />
            <input type='text' name='username' id='username' class='text ui-widget-content ui-corner-all' /><br />
        </fieldset>
    </form>
</div>

Scripting:

 $(document).ready(function() {
    $(":button#forgotpassword").click(function() { //* open dialog to retreive user's password
        $("#dialog-form-lostpass").find("p[id=progress]").text(null); //* reset progress
        $("#dialog-form-lostpass").dialog("open");
        return false;
    });

    $("#dialog-form-lostpass").dialog({
        autoOpen: false,
        modal: true,
        buttons: {
            "mail password": function() {
                $("#dialog-form-lostpass开发者_Go百科").find("p[id=progress]").html("<img src='images/wait.gif' height=10>Contacting Linkshelf Server...");

                $.post(options.engine, {
                    command: "retreivepassword",
                    username: $("#dialog-form-lostpass").find("input[id=username]").val(),
                }, function(data) {
                    if (data.success) {
                        $("#dialog-form-lostpass").dialog("close");
                        $("#loginscreen").find("p[id=progress]").text("Your password is sent to your registered email address. You should receive an email from us soon. Make sure there is no spam-filter blocking our email.").addClass("ui-state-error").removeClass("ui-state-error", 1700);
                    } else {
                        if (data.feedback == "invalid entry") {
                            $("dialog-form-lostpass").find("p[id=progress]").text("Invalid entry. You didn't provide us with enough information to retreive your password").addClass("ui-state-error").removeClass("ui-state-error", 1700);
                        } else {
                            $("#dialog-form-lostpass").find("p[id=progress]").text("Unknown username and/or email address. We can not retreive your password").addClass("ui-state-error").removeClass("ui-state-error", 1700);
                        }
                    }
                }, "json");
            }
        },
    });

});

Now when I hit the 'forgot password' button I get the dialog (check it on this fiddle, layout looks terrible, but you'll get the point). When I hit ESC I go back to the initial screen, when I click 'mail password' nothing unexpectedly happens, but when I hit "enter" to browser loads a different location, as if a default action for the form is executed.

In the fiddle it's hard to see, but in my site people are forwarded to ./?username=, which tells me this is unwanted behavior from the form in the initial screen. How can I prevent this from happening?


In the dialog form add an id:

<form id="passwordform">
        <fieldset>
            <label for='username'>username or email</label><br />
            <input type='text' name='username' id='username' class='text ui-widget-content ui-corner-all' /><br />
        </fieldset>
    </form>

Then tell jQuery to prevent it from submitting since the dialog will do it:

$('#passwordform').submit( function(e) {
     e.preventDefault();
});


Your dialog is closing because when enter is hit the form is submitting, thus re-refreshing the page. To stop this you can add something like.

$('form').submit(function(e){
    return false;
});


First of all you need to have a submit button in the form, otherwise hitting enter doesn't submit it (you can put a hidden submit button - should work also).

And after that you can put a onsubmit="jsProcessFunctionHere(); return false".

This way hitting enter will trigger your password reset procedure, instead of closing the dialog.


My workaround was to put a hidden button before the main button, it seems the first button is the default. So add your buttons like this; just make sure you have a .hide class that has display:none;

        buttons: {

            "Hiden": { text: "", class:"hide", click: function () {
                return false;
            }},
            "Process": { class: "btn btn-primary", text: "OK", click: function () {
                alert('process');
            }
            },
            Cancel: { class: "btn", text: "Cancel", click: function () {
                $(this).dialog("close");
            }}
        }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜