0

「periodType」という名前のラジオ ボタンのグループがあります。そのグループには、追加の div を表示するという事実を除いて、他のボタンと同じように動作するラジオ ボタンが 1 つあります。以下の変更機能は、グループ内のすべてのラジオ ボタンに対して非常に一般的であるため、その特定のラジオ ボタンがいつチェックされているかを知るにはどうすればよいですか。

  $('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>
4

3 に答える 3

1

無線入力に値を追加することができます:

<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>

次に、その値をクエリします。

$('input[name="periodType"]').change(function() {
  if (this.value == 'three' && this.checked) {
    //do something (for all radio buttons)
    //If unique radio button also do this
  }
}); 
于 2012-06-26T12:20:16.450 に答える
0

このようなものを探しているかもしれません。変更時の動作を示すためだけに、各ラジオを独自の div に追加しました。ご不明な点がございましたら、お知らせください。


    <!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>
于 2012-06-26T15:05:36.817 に答える
0
$('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
            }
        }
    });
于 2012-06-26T12:20:47.153 に答える