通过类名JQuery获取ID [英] Get the ID by Class name JQuery

查看:99
本文介绍了通过类名JQuery获取ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,这是一个 jquery 问题:



应该有这个:

 < input type =checkboxclass =selectid =select-1> 

< input type =checkboxclass =selectid =select-2>

,并且我想获取已经选中的复选框的ID (注意:您只能选择1)
,我在这里有此按钮点击事件:

  $(#submit)。click(function(){

if($(。select)。is(':checked')){

alert($(。select)。attr('id'));
}
});

总是提醒select-1,即使我选择了复选框与select-2id ..我猜,因为他们都在同一个类中第一个与.select类被找到显示在警报中..如何获取特定的id通过使用它们的类名检查?谢谢!

解决方案

一次只能选中一个复选框。



alert($(。select:checked)。attr('id'));



<如果您同时选中多个复选框:

  $(。select:checked b $ b alert($(this).attr('id')); 
});

演示: http: //jsfiddle.net/UTux2/


Hi this is a jquery question:

supposed i have this:

<input type="checkbox" class="select" id ="select-1">

<input type="checkbox" class="select" id ="select-2">

and i want to get the id, of the checkbox that has been selected when i click a submit button (note: you can only select 1) and i have this button click event here:

$("#submit").click(function(){

          if($(".select").is(':checked')){

              alert($(".select").attr('id'));
          }
});

It always alerts "select-1" even if i select the checkbox with the "select-2" id.. I'm guessing since they're both in the same class the first one with the ".select" class to be found is displayed in the alert.. how do i get the specific id that is checked by using their class names? Thank You!

解决方案

Are you only checking one checkbox at a time?

alert( $(".select:checked").attr('id') );

Or, if you have multiple checkboxes checked at once:

$(".select:checked").each(function(){
   alert($(this).attr('id'));
});

Demo: http://jsfiddle.net/UTux2/

这篇关于通过类名JQuery获取ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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