Jquery单选从此

Jquery radio selection from this

我正在寻找一种在 jquery

中做类似事情的方法
var my_radio = ("input[name='my_radio']");
my_radio.change(){
    var val = (this":checked").val();
}

我该怎么做? 比

你必须编辑这个:

("input[name='my_radio']");

为此:

$("input[name='my_radio']");

如果您有多个无线电输入,那么您也必须进行以下更改:

my_radio.each(function(){
    $(this).on('change', function(){
        if($(this).is(':checked')){
            console.log($(this).val());
        }
    });
});

示例:

var my_radio = $('input[type="radio"]');
my_radio.each(function(){
        $(this).on('change', function(){
            if($(this).is(':checked')){
                $('span').text($(this).val());
            }
        });
    });
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form>
  <input type="radio" name="1" value="1" />
  <input type="radio" name="1" value="2" />
  <input type="radio" name="1" value="3" />
  <input type="radio" name="1" value="4" />
</form>

<h1>You selected</h1>
<span></span>