如何在更改时使用 jquery 验证输入字段?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4868492/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 18:04:32  来源:igfitidea点击:

How do I use jquery to validate an input field on change?

jqueryinputvalidationonchangeblur

提问by Alex Reynolds

I have an inputfield:

我有一个input领域:

<input type="text" name="notifyEmail" id="parametersEmail" value="" size=40 />

I have a chunk of jquery code that works when I hit tab or otherwise leave the field, which calls a validation routine:

我有一大块 jquery 代码,当我点击 Tab 或以其他方式离开该字段时,它会调用验证例程:

$("#parametersEmail").blur(function(event) {
    validateParameterEmail();
});

What I would like to do is run the validateParameterEmail()function whenever the value or content of the input field changes.

我想做的是validateParameterEmail()每当输入字段的值或内容发生变化时运行该函数。

So I then also tried the .change()handler:

所以我也尝试了.change()处理程序:

$("#parametersEmail").change(function(event) {
    validateParameterEmail();
});

But when I change the contents of parametersEmail, this handler does not call the validation function.

但是当我更改 的内容时parametersEmail,此处理程序不会调用验证函数。

Is there another handler I should be using, instead? Or can I not attach multiple event handlers to the input field?

我应该使用另一个处理程序吗?或者我不能将多个事件处理程序附加到输入字段?

采纳答案by Ish

Try $("#parametersEmail").keydown(function(event) {})

尝试 $("#parametersEmail").keydown(function(event) {})

回答by Victor

Try this:

尝试这个:

$("#parametersEmail").bind('blur', function(event) {} );

and

$("#parametersEmail").bind('keyup', function(event) {} );

回答by FlameStorm

( Reality on almost 2017-th year )

(几乎 2017 年的现实)

The best way is to set once the callback on all need events about input value changing.

最好的方法是在所有有关输入值更改的需要事件上设置一次回调。

There are:

有:

  • keyup : user typed something in the input
  • change : common known method about stored changes in input
  • click : some inputs take changes by mouse too
  • paste : Yea! Ctrl+V, Crtl+Ins, RightMouseButton+Paste can change input value too
  • propertychange : when some JS changes our input at any way, this event will fired
  • input : new standard's event that supports all modern browsers, use it too
  • keyup : 用户在输入中输入了一些东西
  • change :关于输入中存储的变化的公知方法
  • 单击:某些输入也通过鼠标进行更改
  • 粘贴:是的!Ctrl+V、Crtl+Ins、RightMouseButton+Paste 也可以改变输入值
  • propertychange : 当某些 JS 以任何方式更改我们的输入时,将触发此事件
  • input : 支持所有现代浏览器的新标准事件,也可以使用它

So, we can set all of them once:

因此,我们可以一次设置所有这些:

$("#parametersEmail").bind("propertychange change click keyup input paste", function(event) {
    validateParameterEmail();
});

Hope this helps for someone. ;)

希望这对某人有帮助。;)

回答by benhowdle89

Change refers to when you click away/move from the input. You maybe looking for onKeyUp().

更改是指当您单击离开/从输入移开时。您可能正在寻找onKeyUp().