简体   繁体   English

Javascript禁用Enter键

[英]Javascript disable enter key

I'm trying to disable the enter key in a web form. 我正在尝试禁用Web表单中的Enter键。 The web form is set up by a Javascript function "initialize". 该网络表单是由Javascript函数“初始化”设置的。 In that function I added the following code: 在该函数中,我添加了以下代码:

$('html').bind('keypress', function(e)
{
    if(e.keyCode == 13)
    {
        return false;
    }
});

This works well for every button except one. 这适用于除一个按钮以外的所有其他按钮。 On most of my screens I have a "continue" button, set up like this (from an open-source third party library): 在我的大多数屏幕上,都有一个“继续”按钮,其设置如下(从开源第三方库中进行设置):

that.continueButton = function (obj) {

        var str = ""

        if (typeof obj==="string") {
            obj = {label: obj}
        }
        if (!obj) {
            obj = {};
        }
        if (!obj.label) {
            obj.label = Experigen.settings.strings.continueButton;
        }

        str += '<input type="button" value="' + obj.label + '" ';
        var spec = [];
        if (obj.hide===true) {
            spec.push("hide:true");
        }
        if (obj.disable===true) {
            spec.push("disable:true");
        }
        spec = spec.length ? ",{" + spec.join(",") + "}" : "";
        //add answer before advancing       
        str += 'onClick="Experigen.screen().continueButtonClick(this' + spec + ');">'
        return str
    }

    /**
     * Forwards the experiment: calls {@link Experigen.trial.advance} or {@link Experigen.advance} as needed
     * @method
     * @memberof Experigen.trial
     */
    that.continueButtonClick = function (caller, spec) {

        //Add training block information and user responses
        if (Experigen.screen().soundbuttons[0] != undefined)
        {   
            Experigen._screens[Experigen.position].Listens = Experigen.screen().soundbuttons[0].presses;
        }

        Experigen._screens[Experigen.position].Answer = $("input[name=answergiven]").val();
        Experigen._screens[Experigen.position].TrainingBlock = Experigen.settings.training_block;

        var comingFrom = Experigen.screen().findCaller(caller);
        if (comingFrom && comingFrom.attr("class")==="trialpartWrapper") {
            Experigen.screen().advance(spec);
        } else {
            Experigen.advance(caller);
        }

    }

As you might have guessed, the enter key "clicks" this button and moves the user to the next screen. 您可能已经猜到了,输入键“单击”此按钮并将用户移至下一个屏幕。 This is the only button for which the enter key is not disabled. 这是唯一未禁用回车键的按钮。 Does anyone have any ideas as to why this might be? 有谁知道为什么会这样吗?

正如@epascarello在上面的评论中指出的那样,将“ keypress”更改为“ keydown”可以解决问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM