Moji
Moji

Reputation: 1830

backboneJS keydown submits the foem instead of calling the desired function

I am trying to submit a form with keydown(enter) and send it by the function that I want. but when I hit enter, it shows a behaviour like when the type of HTML button is submit. here is my BackboneJS code:

$view.UserProfile_ConfirmPhoneNumber = Backbone.View.extend({
    el: "#confirmPhoneNumberForm",
    events: {
        "click #send-confirm-phone-number-code": "sendConfirmPhoneNumberCode",
        'keydown': 'keyAction',
    },
 keyAction: function (e) {
        var code = e.keyCode || e.which;
        if (code == 13) {
            sendConfirmPhoneNumberCode();
        }
    },
    sendConfirmPhoneNumberCode: function() {
//some code
}

here is my html:

<form class="newRegister-formBox basketForm" id="confirmPhoneNumberForm" autocomplete="off">

            <input type="hidden" id="returnUrl" value="@ViewBag.ReturnUrl" name="ReturnUrl">
            <div class="newRegister-formBox">
                <div class="formElement newRegister-input">
                    <i class="fa fa-key loginIcone"></i>
                    <input autocomplete="off" type="text" name="code" id="code" class="form-control persianDigitInput" placeholder="enter code">
                </div>
                <div class="newRegister-button">
                    <button type="button" id="send-confirm-phone-number-code" class="animateBtn greenAnimateBtn">
                        <i class="fa fa-check"></i>
                        confirm
                    </button>
                </div>
            </div>
    </form>

Upvotes: 0

Views: 20

Answers (1)

T J
T J

Reputation: 43166

Try

 keyAction: function (e) {
    var code = e.keyCode || e.which;
    if (code == 13) {
        e.preventDefault();
        sendConfirmPhoneNumberCode();
        return false;
    }
},

Upvotes: 1

Related Questions