backboneJS keydown提交foem,而不是调用所需的函数

zpjtge22  于 2022-11-10  发布在  其他
关注(0)|答案(1)|浏览(140)

我尝试用keydown(enter)提交一个表单,并通过我想要的函数发送它。但是当我按回车键时,它显示的行为就像HTML按钮的类型是submit一样。下面是我的BackboneJS代码:

$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
}

下面是我的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>
krcsximq

krcsximq1#

尝试

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

相关问题