简体   繁体   English

按Enter键时触发了两个事件

[英]Two Events Fired when I press enter key

In my code, I have the following javascript code block in the header section 在我的代码中,标题部分具有以下javascript代码块

    function validateForm(bid) {
        switch (bid) {
        case "submitIDSearch":
            alert("submitIDSearch");
            return false;
        case "submitNameSearch":
            alert("submitNameSearch");
            return false;
        }
    }
    function fKeyDown(e) {
        var  kc = window.event ? window.event.keyCode : e.which;
        if (kc == 13) {
            document.getElementById('submitNameSearch').click();
        }
    }

Then I have the following HTML code block 然后我有以下HTML代码块

    <form name="checkAbsenceForm" method="post" action="absenceReport.htm" onsubmit="return validateForm(this.submited)">
        <label class="q">ID * <input id="searchRUID" name="searchID" maxlength="9" /></label>
        <input id="submitIDSearch" type="submit" value="Search ID" onclick="this.form.submited = this.id;" />
        <hr />
        <label class="q">First Name <input id="searchFirstName" name="searchFirstName" maxlength="23" onKeyDown="javascript:fKeyDown(event);"/></label>
        <br />
        <label class="q">Last Name * <input id="searchLastName" name="searchLastName" maxlength="23" onKeyDown="javascript:fKeyDown(event);" /></label>
        <input id="submitNameSearch" type="submit" value="Search Name" onclick="this.form.submited = this.id;" />
    </form>

What happened was that when I press Enter key in the searchLastName input text box, both alert message box pops up. 发生的事情是,当我在searchLastName输入文本框中按Enter键时,两个警报消息框均弹出。 One showing submitNameSearch and the other showing submitIDSearch . 一个显示submitNameSearch ,另一个显示submitIDSearch submitNameSearch is the desired event, but submitIDSearch is not, I think it is somehow triggered by default. submitNameSearch是所需的事件,但是submitIDSearch不是,我认为默认情况下会触发该事件。

May I ask if there's a way to get rid of the submitIDSearch event when I press Enter key in the searchLastName input text box? 我可以问一下在searchLastName输入文本框中按Enter键时是否有一种方法可以摆脱submitIDSearch事件吗?

Thanks a lot! 非常感谢!

When you press Enter in a form, the form is submitted. 当您在表单中按Enter键时,将提交表单。 That's the reason of the second call to validateForm . 这就是第二次调用validateForm的原因。

Two solutions : 两种解决方案:

1) Remove the onKeyDown="javascript:fKeyDown(event);" 1)删除onKeyDown="javascript:fKeyDown(event);" to have the normal validation defined on onsubmit=... apply. 具有onsubmit=...上定义的常规验证。

2) In fKeyDown , add e.preventDefault(); 2)fKeyDown ,添加e.preventDefault(); to prevent the default handling of the key event : 防止默认处理key事件:

function fKeyDown(e) {
    var  kc = window.event ? window.event.keyCode : e.which;
    if (kc == 13) {
        document.getElementById('submitNameSearch').click();
        e.preventDefault();
    }
}

But if you really do just this in fKeyDown , solution 1 is enough. 但是,如果您真的只是在fKeyDown这样做,则解决方案1就足够了。

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

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