通过类名 JQuery 获取 ID

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19582698/
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-27 00:14:19  来源:igfitidea点击:

Get the ID by Class name JQuery

javascriptjqueryclassinputcheckbox

提问by muffin

Hi this is a jqueryquestion:

您好,这是一个jquery问题:

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 checkboxthat has been selected when i click a submit button (note: you can only select 1) and i have this button click event here:

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

$("#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!

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

回答by XCS

Are you only checking one checkbox at a time?

您一次只选中一个复选框吗?

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

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/

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

回答by Elon Than

Get ID using this way

使用这种方式获取ID

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

Also keep in mind that if only one can be selected, it's better to change it to radio.

还要记住,如果只能选择一个,最好将其更改为radio.

回答by Somnath Kharat

$("#submit").click(function(){  
          if($(".select").is(':checked')){
               var $this=$(".select").is(':checked');
               alert($this.attr('id'));
          }
});

回答by sudhAnsu63

There are multiple elements. You need to check for all checkbox having same class

有多种元素。您需要检查所有具有相同类的复选框

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

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

});

回答by Dementic

you should be using radio buttons and not checkboxes to allow one choice out of many.

您应该使用单选按钮而不是复选框来允许从众多选择中选择一个。

<input type="radio" name="select" value="select-1">
<input type="radio" name="select" value="select-2">

then things will get really simple:

那么事情会变得非常简单:

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

    alert($('[name="select"]:checked').val());
});

回答by MrCode

You aren't capturing the checked checkbox, you're only asking "is there one checked?".

您没有捕获选中的复选框,您只是在问“是否选中了一个?”。

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

    var elem = $(".select:checked");

    if(elem.length > 0){
        alert(elem.attr('id'));
    }
});

回答by rajesh kakawat

try something like this, you don't need to iterate

尝试这样的事情,你不需要迭代

        $(document).ready(function () {
            $("#submit").click(function(){
                  if($(".select").is(':checked')){
                      alert($(".select:checked").attr('id'));
                  }
            });
        });

回答by swapnil

Because you have not iterated all the elements which has class 'select'. It always goes to the first element and prints the result. Put a loop to iterate all elements having class 'select'.

因为您还没有迭代所有具有“select”类的元素。它总是转到第一个元素并打印结果。放置一个循环来迭代所有具有“select”类的元素。