Jquery在具有相同id的多个输入中检测按键 [英] Jquery detect keypress in multiple inputs with same id

查看:86
本文介绍了Jquery在具有相同id的多个输入中检测按键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一堆具有相同ID但不同名称的输入框如下:

So I have a bunch of input boxes with the same id but different names like so:

<input type="text" id="description" name="1">
<input type="text" id="description" name="2">
<input type="text" id="description" name="3">

现在对于一个输入框(只有一个框使用id)我使用:

Now for a single input box (only one box uses the id) I use:

$('#description').keypress(function(....

我希望能够做的是使用上面的函数然后根据输入框的名称做一些事情,但是对于多个输入框来执行相同的ID。这有可能吗?

What I would ideally like to be able to do is use the above function and then do something based on the name of the input box but for multiple input boxes to carry the same id. Is this possible in some way?

推荐答案

你应该为html元素设置唯一的id,你应该分配一个公共类和通过它访问。

You should have unique id for html elements you should assign a common class and access throug it.

现场演示

Live Demo

<input type="text" id="description1" name="1" class="commonclass">
<input type="text" id="description2" name="2" class="commonclass">
<input type="text" id="description3" name="3" class="commonclass">


 $('.commonclass').keypress(function(event){
       alert("keycode: " + event.keyCode);
       alert("id: " + this.id);
 });​

这篇关于Jquery在具有相同id的多个输入中检测按键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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