可能阻止输入从仅在某些输入字段的JavaScript中提交表单 [英] Possible to Prevent Enter From Submitting a Form in Javascript From Certain Input Fields Only

查看:175
本文介绍了可能阻止输入从仅在某些输入字段的JavaScript中提交表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个窗体,onkeyup会触发某些字段的建议框的创建。我可以捕获向上箭头,向下箭头和逃逸的击键,并相应地使用它们在建议框中移动或关闭它。我想用enter将它们的选择移到触发建议的输入字段中。但是,我似乎无法捕获输入按键。

控制永远不会进入我的JavaScript建议功能。而是表单简单地提交。我试过noEnter函数返回false时输入传入onkeypress和onkeydown问题的输入元素,没有骰子。该表单似乎从所有输入事件处理程序中获取控件,并在其中的任何一个可以执行任何操作之前提交自己。

表单提交是否会覆盖字段中的所有其他事件处理程序,或者是否还有其他内容正在执行?如果是这样,是否有解决方法?



以下是有问题的元素(删除了一些值):

 < input type =textname =myInputid =myInputFieldsize =22onkeypress =noEnter(event)onkeyup =suggest (event)onblur =hideSuggestions()> 

这里是javascript来捕捉它:

  var evnt =(event?event:window.event); 

var key = evnt.keyCode;

if(key == 13){//处理输入
alert(ENTER PRESSED。);
hideSuggestions();
返回false;
}

警报永不会触发。警报放在函数的顶部也不会触发。然而,对于转义键,如果在检查键== 27 时将相同的代码放在一起,则警报将触发。



在几乎相同的主题上还有其他问题,例如:阻止用户通过输入#2来提交表单

然而,我不使用jquery,我只想阻止提交为这一个领域。但是,在各种问题中提到的其他解决方案似乎都没有效果。



EDITTED:解释密钥设置的位置。

解决方案

在onkeypress事件中,尝试更改输入上的onkeyup事件以返回函数结果



 < input type =textname =myInputid =myInputFieldsize =22onkeypress =return dontSubmit事件); /> 

然后是JS:



<$ p $函数dontSubmit(event)
{
if(event.keyCode == 13){
return false;
}
}


I have a form for which onkeyup triggers the creation of a suggestions box for certain fields. I am able to capture the keystrokes for the up arrow, for the down arrow and for escape and use them accordingly to move around in the suggestions box or close it. I would like to use enter to move their selection into the input field that triggered the suggestions. However, I seem to be unable to capture the enter keystroke.

Control never enters my javascript suggest function. Rather the form simply submits. I've tried noEnter functions that return false on enter passed into onkeypress and onkeydown for the input element in question, no dice. The form seems to grab control from all of the input event handlers and submit itself before any of them can do anything.

Does form submit override all other event handlers in fields, or is there something else going on here? If it does, is there some way around that?

Here's the element in question (some values removed):

<input type="text" name="myInput" id="myInputField" size="22" onkeypress="noEnter(event)" onkeyup="suggest(event)" onblur="hideSuggestions()">

And here's the javascript to catch it:

    var evnt = (event ? event : window.event);

    var key = evnt.keyCode;

    if(key == 13) { // Handle Enter
        alert("ENTER PRESSED.");
        hideSuggestions();
        return false;
    }

The alert never fires. Alerts put at the top of the function never fire either. For the escape key however, when I place the exact same code in an if checking for key == 27 the alert fires.

There are other questions on almost the same topic, such as this one: Prevent Users from submitting form by hitting enter #2

However, I am not using jquery and I only want to prevent enter from submitting for this one field. But none of the other solutions mentioned in various questions seem to work.

EDITTED: To explain where key is set.

解决方案

try changing the onkeyup event on the input to return the function result

so in the onkeypress event:

<input type="text" name="myInput" id="myInputField" size="22" onkeypress="return dontSubmit(event);" />

and then the JS:

function dontSubmit (event)
{
   if (event.keyCode == 13) {
      return false;
   }
}

这篇关于可能阻止输入从仅在某些输入字段的JavaScript中提交表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆