Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pressing 'Enter' executes Cancel button instead of Submit

Tags:

angularjs

<form class="stdform stdform2" name="projectForm" ng-submit="saveProject(project, projectForm)"
      novalidate="novalidate">
    <p>
        <div class="par control-group" ng-class="{'has-error': projectForm.projectName.$invalid && (projectForm.projectName.$dirty || submitted)}">
        <label class="control-label" for="projectname">Name</label>

        <div class="controls">
        <span class="field">
            <input id="projectname" type="text" ng-model="project.name" name="projectName" class="form-control"
                   required/>
            <label ng-show="projectForm.projectName.$error.required && (projectForm.projectName.$dirty || submitted)"
                   class="error" for="projectname">Please type project name</label>
        </span>
        </div>
    </div>
    </p>
    <p>
        <label>Description</label>
        <span class="field">
            <textarea ng-model="project.description" name="projectDescription"
                      style="resize: vertical; height: 110px;" class="form-control input-default"></textarea>
        </span>
    </p>

    <p class="stdformbutton">
        <button class="btn btn-default" ui-sref="app.project.list">Cancel</button>
        <button class="btn btn-primary" type="submit">{{saveMessage}}</button>
    </p>
</form>

Why after pressing the 'Enter' key on keyboard am I redirected to app.project.list instead of having the saveProject(project, projectForm) function executed?

like image 468
pmichna Avatar asked Dec 25 '22 08:12

pmichna


1 Answers

The default type for buttons is submit. You have to explicitly unset it to "button" to avoid the submit action:

<p class="stdformbutton">
    <button class="btn btn-default" type="button" ui-sref="app.project.list">Cancel</button>
    <button class="btn btn-primary" type="submit">{{saveMessage}}</button>
</p>

You can also set the type="reset" if you want it to clear out the input values.

like image 125
musically_ut Avatar answered Dec 28 '22 07:12

musically_ut