Why does my jQuery dialog reload the page when I press enter?

I am trying to use the jQuery dialog to enter data on my page. For some reason, I don’t understand when the user presses the enter button, the whole page is refreshed. It does not even check what is in the text box.

I created a jsfiddle that shows the problem. I checked the documentation here and my code seems to follow the recommendations, but I have to skip something!

This is how I call the dialogue

$("#create-subtitle")
    .click(function () {
    $("#dialog-form-subtitles").dialog("open");
    return false;
});

Here is one of my dialogs:

$("#dialog-form-steptitles").dialog({
        autoOpen: false,
        height: 220,
        width: 450,
        modal: true,
        buttons: {
            "Ajouter un sous-titre pour les étapes": function () {
                var bValid = true;
                allFieldsStepTitle.removeClass("ui-state-error");
                bValid = bValid && checkLength(nameStepTitle, "sous-titre pour les étapes", 3, 50);

                if (bValid) {
                    var $parent = $("#StepTitles");
                    var numElem = $parent.find("tr").length;
                    $('#StepTitles > tbody:last').append('<tr><td><input class="text-box single-line" id="StepTitles_' + numElem + '__Name" name="StepTitles[' + numElem + '].Name" type="text" value="' + nameStepTitle.val() + '" /></td><td>&nbsp;<ul id="ListStep' + numElem + '"></ul></td><td><button id="create-step' + numElem + '" name="create-step' + numElem + '" class="btn btn-success">Ajouter une étape</button></td></tr>');
                    $(this).dialog("close");
                }
            },
            Cancel: function () {
                $(this).dialog("close");
            }
        },
        close: function () {
            allFieldsStepTitle.val("").removeClass("ui-state-error");
        }
    });

Can anybody help me?

+5
source share
2 answers

jQuery form, . . , , SO.

+1

enter, form > input , . , , . , , , , :

$('form').on('submit', function(event){
    event.preventDefault();
});

, , - - , , , .

$('your-input').keypress(function(event) {
    if(event.which == 13) { // 13 is the 'Enter' key
     // do something here
   }
});

, .

+5

All Articles