在jQuery中使用多个ID [英] using multiple id in jquery

查看:156
本文介绍了在jQuery中使用多个ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我制作了一些小小的jquery脚本来检查输入框的值是否大于5.但是我有2个带有id的标签,并且只有其中一个起作用.

I made little jquery script for checking if input box value bigger than 5 .but I have 2 tag with an id and only one of them works.

<div id="register">
    <form id="register">
<input id="first" type="text" /> <a id="first" ></a> <br />
    <input id="second" type="text" /> <a id="second" ></a>

</form>

$('input').keyup(function(){
if($(this).val().length<5)
   {
  $('a.first').text("Please fill"); 
   }
if($(this).val().length<5){
     $('a.second').text("Please fill"); 
   }
});

但是它仅显示第一个<a id="first"></a>标记.第二个标签不可见

But it shows only first <a id="first"></a> tag. Second tag not visible

推荐答案

您只需要更改<a>即可使用类:

You just need to change your <a>'s to use classes:

<div id="register">
    <form id="register">
       <input id="first" type="text" /> <a class="first" ></a> <br />
       <input id="second" type="text" /> <a class="second" ></a>
    </form>
</div>

您正在使用的jQuery选择器:

The jQuery selectors you're using:

$('a.first')
$('a.second')

正在寻找带有类的<a>.如果您要查找具有ID的<a>,则它们必须如下所示:

Are looking for an <a> with a class. If you were trying to look for an <a> with an id, they would need to be as follows:

$('a#first')
$('a#second')

这篇关于在jQuery中使用多个ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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