Javascript 获取Jquery中单选按钮组的id

Javascript 获取Jquery中单选按钮组的id,javascript,jquery,Javascript,Jquery,我有一组名为“periodType”的单选按钮。该组中有一个单选按钮的行为与其他单选按钮相同,只是它显示了一个额外的div。我如何知道何时选中了该单选按钮,因为下面的更改功能对于该组中的所有单选按钮都是非常通用的: $('input[name="periodType"]').change(function() { if(this.checked) { //do something (for all radio buttons)

我有一组名为“periodType”的单选按钮。该组中有一个单选按钮的行为与其他单选按钮相同,只是它显示了一个额外的div。我如何知道何时选中了该单选按钮,因为下面的更改功能对于该组中的所有单选按钮都是非常通用的:

  $('input[name="periodType"]').change(function() {
        if(this.checked) {
            //do something (for all radio buttons)
            //If unique radio button also do this
        }
    }); 

    <input type="radio" name="periodType" default>
<input type="radio" name="periodType" default>
<input type="radio" name="periodType" default>
<input type="radio" name="periodType" default>
$('input[name=“periodType”]”)。更改(函数(){
如果(选中此项){
//执行某些操作(针对所有单选按钮)
//如果是唯一的单选按钮,也可以执行此操作
}
}); 

您可能需要为收音机输入添加一个值:

<input type="radio" name="periodType" value="one" default>
<input type="radio" name="periodType" value="two" default>
<input type="radio" name="periodType" value="three" default>
<input type="radio" name="periodType" value="four" default>

也许你在找这样的东西。我将每台收音机添加到自己的div中,只是为了显示变化时的行为。如果你有任何问题,请告诉我



.所选{背景色:#fad42e;}
.notSelected{背景色:#6f6e73;}
$(文档).ready(函数(){
$('input[name=“periodType”]”)。更改(函数(){
$(this.parent('div').removeClass();
$(this).parent('div').addClass('selected');
$('input[name=“periodType”]”)
.parent(“div”)
.not($(this.parent(“div”))
.每个功能(e){
$(this.removeClass();
$(此).addClass(“未选定”);
});
});
});

我在html中看不到任何div,所以其他div在哪里displayed@user1038814嗨,伙计,如果我的解决方案对你有帮助,请将答案标记为正确。谢谢
$('input[name="periodType"]').change(function() {
  if (this.value == 'three' && this.checked) {
    //do something (for all radio buttons)
    //If unique radio button also do this
  }
}); 
$('input[name="periodType"]').change(function() {
        if(this.checked) {
            //do something (for all radio buttons)
            //If unique radio button also do this
           var idval = $(this).attr("id");
           if (idval == "something")
           {
             //do something
            }
        }
    });
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
            "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
        <title></title>
        <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>

        <style type="text/css">
            .selected{background-color: #fad42e;}
            .notSelected{background-color: #6f6e73;}
        </style>
        <script type="text/javascript">


            $(document).ready(function () {


                $('input[name="periodType"]').change(function() {
                    $(this).parent('div').removeClass();
                    $(this).parent('div').addClass('selected');

                    $('input[name="periodType"]')
                            .parent('div')
                            .not($(this).parent("div"))
                            .each(function(e){
                                $(this).removeClass();
                                $(this).addClass("notSelected");
                            });
                });



            });




        </script>
    </head>
    <body>

    <div>
        <input type="radio" name="periodType" >
    </div>
    <div>
        <input type="radio" name="periodType" >
    </div>
    <div>
        <input type="radio" name="periodType" >
    </div>
    <div>
        <input type="radio" name="periodType" >
    </div>

    </body>
    </html>