jQuery 单选按钮检查事件处理

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

Radio button checked event handling

jqueryradio-buttonchecked

提问by tarique

In my application, I need a radio group, in which whenever a radio-button is checked, an alert occur so that I can post it's value to ajax post with jQuery.

在我的应用程序中,我需要一个无线电组,只要选中一个单选按钮,就会发生警报,​​以便我可以将其值将其值发布到使用jQuery的Ajax Post。

Can you help me please how i can do it in jQuery?

你能帮我看看我如何在jQuery中做到这一点吗?

回答by Sarfraz

Try something like this:

尝试这样的事情:

$(function(){
  $('input[type="radio"]').click(function(){
    if ($(this).is(':checked'))
    {
      alert($(this).val());
    }
  });
});

If you give your radio buttons a class then you can replace the code $('input[type="radio"]')with $('.someclass').

如果你给你的单选按钮一个类,那么你可以$('input[type="radio"]')$('.someclass').

回答by David Murdoch

Update in 2017: Hey. This is a terrible answer. Don't use it. Back in the old days this type of jQuery use was common. And it probably worked back then. Just read it, realize it's terrible, then move on (or downvote or, whatever) to one of the other answers that are better for today's jQuery.

2017 年更新:嘿。这是一个可怕的答案。不要使用它。在过去,这种类型的 jQuery 使用很常见。它可能在当时有效。只需阅读它,意识到它很糟糕,然后继续(或否决或其他)对今天的 jQuery 更好的其他答案之一。



$("input[type=radio]").change(function(){
    alert( $("input[type=radio][name="+ this.name + "]").val() );
});

回答by e-info128

The HTML code:

HTML代码:

<input type="radio" name="theName" value="1" id="option-1">
<input type="radio" name="theName" value="2">
<input type="radio" name="theName" value="3">

The Javascript code:

Javascript代码:

$(document).ready(function(){
    $('input[name="theName"]').change(function(){
        if($('#option-1').prop('checked')){
            alert('Option 1 is checked!');
        }else{
            alert('Option 1 is unchecked!');
        }
    });
});

In multiple radio with name "theName", detect when option 1 is checked or unchecked. Works in all situations: on click control, use the keyboard, use joystick, automatic change the values from other dinamicaly function, etc.

在名为“theName”的多电台中,检测何时选中或取消选中选项 1。适用于所有情况:点击控制、使用键盘、使用操纵杆、自动更改其他动态功能的值等。

回答by Radames E. Hernandez

You can simply use the method changeof JQueryto get the value of the current radio checked with the following code:

你可以简单的使用方法changeJQuery的获取当前的无线电使用下面的代码检查的价值:

JQuery:

查询:

$(document).on('change', '[type="radio"]', function() {
    var currentlyValue = $(this).val(); // Get the radio checked value

    alert('Currently value: '+currentlyValue); // Show a alert with the current value
});

You can change the selector '[type="radio"]'for a class or id that you want.

您可以更改'[type="radio"]'您想要的类或 ID的选择器。

Regards!

问候!

回答by mital korat

$("#expires1").click(function(){
     if (this.checked)
        alert("testing....");
});

回答by Lu Blue

$(document).on('change','.radio-button', function(){

    const radio = $(this);

    if (radio.is(':checked')) {
      console.log(radio)
    }

});